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

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -