Custom method on association?

You can of course add methods to the User class and you can also extend associations (which isn't quite the same thing).
Tell us a little bit more about what you are doing and what is going wrong and I'm sure someone can help you.

Fred

Well, as I said before, I get an "undefined method" error. The code I
showed you was in my show.html.erb file, and the controller code is in
the groups_controller. I put the code for .groupwide_score() in the
Groups model. Here's what I got (ignore the SQL, I'm awful at it and
still haven't debugged it):

  def groupwide_score(league)
    query = "select * from
    guesses g,
    games ga
    where ga.id = g.game_id
    and ga.league_id = ?
    and g.user_id = ?"
    user_guesses = Guess.find_by_sql([query, id, league])
    results = user_guesses.map { |g| g.points }.sum
  end

I'm not sure why rails can't seem to find the method, the model the
correct place for it, right? Or should I put it in the User model?

Frederick Cheung wrote:

Well, as I said before, I get an "undefined method" error. The code I
showed you was in my show.html.erb file, and the controller code is in
the groups_controller. I put the code for .groupwide_score() in the
Groups model. Here's what I got (ignore the SQL, I'm awful at it and
still haven't debugged it):

def groupwide_score(league)
   query = "select * from
   guesses g,
   games ga
   where ga.id = g.game_id
   and ga.league_id = ?
   and g.user_id = ?"
   user_guesses = Guess.find_by_sql([query, id, league])
   results = user_guesses.map { |g| g.points }.sum
end

I'm not sure why rails can't seem to find the method, the model the
correct place for it, right? Or should I put it in the User model?

If it's in the group model then member.user.groupwide_score can't
possibly work, because user is an instance of User, not Group
(assuming you are using sane conventions). This is all rather
tautological, but if you want to call a method on an instance of User
you need to define it on user (or its superclass, a module included in
it etc...)

Fred

I'll be the first to admit that I had to look it up...

tautology, n.

1. needless repetition of an idea, esp. in words other than those of the immediate context, without imparting additional force or clearness, as in "widow woman."
2. an instance of such repetition

http://dictionary.reference.com/browse/tautological

Peace,
Phillip

Frederick Cheung wrote: