ruby - Self-reference in a Rails Model -
let's have 2 models: user , point.
point belongs_to user. point has 'amount'. want define method in point model return user's total points. i'd call in such manner: user.points.total
i don't know how define method in point model in such way don't have pass user's id. i'd imagine there simple way this, googlefu failing me.
assuming user model has
has_many :points
you can in user model
def total_points points.sum(:amount) end
Comments
Post a Comment