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

Popular posts from this blog

java - How to Configure JAXRS and Spring With Annotations -

visual studio - TFS will not accept changes I've made to a Java project -

php - Create image in codeigniter on the fly -