On Aug 19, 2010, at 12:50 PM, Robert Walker wrote:
> nirosh wrote:
>> i have a poem model. i need to display the most viewed poems.
>> is there any plug inns out there to do this?
>
> Unless I'm missing something obvious, I wouldn't think a plugin would be
> needed for this.
>
> poems_controller.rb
> --------------------
> def show
> @poem = Poem.find(params[:id])
> @poem.increment!(:view_count)
Don't use increment. It won't do what you want since it's only incrementing
the value of the object and then saving it. Not updating the value in the
database. That is to say...
>> u = User.find(2)
>> u1 = User.find(2)
>> u.login_count
=> 7
>> u1.login_count
=> 7
>> u1.increment!(:login_count)
>> u1.login_count
=> 8
>> u.login_count
=> 7
>> u.increment!(:login_count)
>> User.find(2).login_count
=> 8
Oops!
You want to use increment_counter which calls update_counters which does this
in the database:
UPDATE poem SET view_count = view_count + 1 WHERE id = 123
So... you want this:
def show
Poem(:view_count, params[:id])
end
> ...
> ...
> end
>
> http://railsapi.com/doc/rails-v2.3.8/classes/ActiveRecord/Base.html#M001117
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.