Say you have an app not dissimilar to StackOverflow where users vote on
posts. Say an Up Vote causes the voter to receive one reputation point. You
might see something like this in the vote_up model:

class UpVote < ActiveRecord::Base
belongs_to :user
after_create :increase_user_reputation

private

def increase_user_reputation
user = self.user
user.reputation == user.reputation + 1
end
end

There is one issue with this code: The value 1 is hard coded. Where does
such a value belong? Another issue is that some UpVote should have no
knowledge of user.reputation. We solve this by changing the callback:

def increase_user_reputation
self.user.modify_reputation(1)
end

Then we add an instance method to User:

def modify_reputation(reputation)
self.reputation = self.reputation + reputation
self.save!
end

Questions...

1. Who's responsible for calling save! ? The *increase_user_reputation*inside the UpVote model or the
*modify_reputation* method inside the user model?
2. The +1 one to reputation doesn't seem like a good idea to hard code.
Where does Rails keep such configuration settings?
3. Does the code look reasonable? Are there any shortcuts?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/_bQDSP0JAhsJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Mohamad El-Husseini at Jan 31, 2012 at 11:48 am
    Just a bump!

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/QLo8XdPmGJgJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 30, '12 at 2:56p
activeJan 31, '12 at 11:48a
posts2
users1
websiterubyonrails.org
irc#RubyOnRails

1 user in discussion

Mohamad El-Husseini: 2 posts

People

Translate

site design / logo © 2022 Grokbase