ruby on rails - Making sum on join tables -
i creating rails 3.2 web app. in app got 4 tables. project, task, article , item.
what want task values (prices article) summed in single call.
this tried , works, best way of doing it?
@project.tasks.where("status = ?", "completed").joins(:articles).sum(:price)
task table
class task < activerecord::base has_many :articles has_many :items, :through => :articles end
article join table
class article < activerecord::base belongs_to :task belongs_to :item attr_accessible :account_id, :amount, :price, :item_id, :task_id end
item table
class item < activerecord::base has_many :articles has_many :tasks, :through => :articles end
to sum looks ok way did it, can prettify code:
project.rb
has_many :completed_tasks, class: 'task', :conditions => {:status => 'completed'}
controller
@project.completed_tasks.joins(:articles).sum(:price)
Comments
Post a Comment