Difference between revisions of "Talk:Workbook"
Line 12: | Line 12: | ||
− | Class UserController < ApplicationController def view | + | |
+ | Class UserController < ApplicationController | ||
+ | def view | ||
+ | unless @user == current_user | ||
+ | session[:visited_profiles] << user_profile.id unless session[:visited_profiles].include?(user_profile.id) | ||
end | end | ||
end | end |
Latest revision as of 19:45, 7 April 2008
counting User Profile views
set up a table for the view count as follows:
1
create_table :user_views do |t| t.column :user_profile_id, :integer t.column :count, :integer, :default => 1 t.column :created_at, :datetime end
2- create controller called User,
Class UserController < ApplicationController
def view unless @user == current_user session[:visited_profiles] << user_profile.id unless session[:visited_profiles].include?(user_profile.id) end end
end
3-add session class Session < ActiveRecord::Base before_destroy :update_page_views def update_page_views visited = CGI::Session::ActiveRecordStore::Session.unmarshal(Session.find(:first).data)[:visited_profiles] return if visited.blank? visited.each do |p| UserView.increment_or_create(:user_profile_id => p) end end
4- go in to model => user view => increment_or_create and add this code.
def self.increment_or_create(attributes={})
if (view = self.find_by_user_profile_id(attributes[:user_profile_id])) view.update_attributes(:count => (view.count +=1)) else self.create(attributes) end end