Monday, 19 August 2013

Self-reference in a Rails Model

Self-reference in a Rails Model

Let's say I have two models: User and Point.
Point belongs_to the user. Point has an 'amount'. I want to define a
method in the Point model that will return the User's total points. I'd
like to call it in such a manner: User.points.total
I just don't know how to define the method in the Point model in such a
way that I don't have to pass it the User's ID. I'd imagine there is a
simple way to do this, but my Googlefu is failing me.

No comments:

Post a Comment