Friday, 15 February 2013

ruby on rails - Association Clarification -



ruby on rails - Association Clarification -

was wondering if assist this

i have next associations

user - has_many :campaigns campaign - has_many :donations belongs_to :user donation - belongs_to :campaign

as user sum of donations made campaigns current_user has created, tried this

@campaigns = current_user.campaigns @total_donations = @campaigns.dontations.sum('donation_amount')

but not simple as have loop through each of campaigns belonging user dont i?

whats best way approach this? scope or instance method improve also?

any help appreciated

a has_many :through association should work:

class user < activerecord::base has_many :campaigns has_many :donations, through: campaigns end current_user.donations.sum(:donation_amount)

ruby-on-rails ruby ruby-on-rails-4 associations

No comments:

Post a Comment