Custom method on association?

Hi, I’m trying to do a custom method, but I get an undefined method
error. Here’s what’s happening:

<% @members.each do |member| %>

<%= member.user.name %>
<%= member.user.groupwide_score(@league) %>

<% end %>

Can I add the method groupwide_score() to member.user, or will Rails
think that I’m trying to find a column in the users table? Is there a
way to do this?

Thanks!

Dave

you can use instance_eval, class_eval or define_method to do that
dynamically. Or just add it to the class via open objects.

If this isn’t helpful enough could you elaborate a little more.

class attributes?
one instance or whole class?
just this class or any class?
.
.
.

On 24 Jan 2008, at 02:56, Dave A. wrote:

Can I add the method groupwide_score() to member.user, or will Rails
think that I’m trying to find a column in the users table? Is there a
way to do this?

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 C. wrote:

On 24 Jan 2008, at 02:56, Dave A. wrote:

Can I add the method groupwide_score() to member.user, or will Rails
think that I’m trying to find a column in the users table? Is there a
way to do this?

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

On 24 Jan 2008, at 15:26, Dave A. wrote:

games ga

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

On Jan 24, 2008, at 9:44 AM, Frederick C. wrote:

tautological

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

Peace,
Phillip

Frederick C. wrote:

On 24 Jan 2008, at 15:26, Dave A. wrote:

games ga

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

Okay, thanks! I’m still kinda new to programming, so some of that
probably obvious logic isn’t so obvious to me. But I moved it to the
User model and it worked like a charm. Thanks so much!

Dave A. wrote:

Hi, I’m trying to do a custom method, but I get an undefined method
error. Here’s what’s happening:

<% @members.each do |member| %>
<%= member.user.name %>
<%= member.user.groupwide_score(@league) %>

<% end %>

Can I add the method groupwide_score() to member.user, or will Rails
think that I’m trying to find a column in the users table? Is there a
way to do this?

Thanks!

Dave

Simple answer is YES. Better answer is do you want to? The rest of the
thread seems to be debating the second part. To do the first:

class Member
belongs_to :user, :options_go_here do
def groupwide_score(league)
#Go do that Voodoo that you do so well!
end
end
end

This is one of two ways to do exactly what you want. For more
information check_out
http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html
and look in the “Association extensions” in the introduction.

You can also check out my slightly long post about them here:
http://startrader.wordpress.com/2008/01/18/three-great-features-of-rails/