ruby on rails 3 - Sum column values under condition -
suppose this:
works table
name created_at size
i want list works , in end sum size values, under 1 condition. created_at must < 1 year ago.
i know
@works.sum(&:size)
works doesn't filter out 'created_at' part.
then got @works.sum(&:size, :conditions => ['created_at > ?', 1.year.ago])
but keep getting compile error expecting ) instead of ,
help please? better way accomplish this?
you can use scope keep model clean
#controller class workscontroller < applicationscontroller def index @works.recent.sum(&:size) end end #model class work scope :recent, ->{ where('created_at > ?', 1.year.ago) } end
Comments
Post a Comment