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 - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -