Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get records where date.year == some_year in Rails

I have a date column in my database in a Rails 3.1 app, and I want to be able to get the records where the date's year matches a specific year. I tried where(:date.year == year) but of course I got NoMethodError: undefined method 'year' for :date:Symbol. Is it possible to do this type of query?

like image 619
SZH Avatar asked Dec 27 '22 18:12

SZH


1 Answers

You can use a scope to build something like:

scope :for_year, lambda {|date| where("date >= ? and date <= ?", "#{date.year}0101", "#{date.year}1231")}
like image 93
Jesse Wolgamott Avatar answered Jan 08 '23 21:01

Jesse Wolgamott