Custom method on association?

Dave Amos 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| %>     <td><%= member.user.name %></td>     <td><%= member.user.groupwide_score(@league) %></td>     <td></td> <% 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/