Reputation: 13
I am trying to create a scope which can find the dataset for example, less_than_a_year, less_than_six_month, in my model through my deadline attribute.
class Goal < ActiveRecord::Base
attr_accessible :category, :title, :detail, :deadline, :achieve
#need help solving this
scope :less_than_a_year, where()
end
So it would perform, goal.less_than_a_year and provide a list of data less then a year. Thank you.
Upvotes: 1
Views: 2789
Reputation: 3809
I'm pretty sure this is database specific, but it should work for most.
class Goal < ActiveRecord::Base
scope :less_than_a_year, lambda{ where("deadline < ?", 1.year.from_now) }
end
This assumes that deadline is a Date
or DateTime
.
Edit: added lambda (i swear i did this before I saw the other answers, but not before the downvote)
Upvotes: 0
Reputation: 11167
class Goal < ActiveRecord::Base
#need help solving this
scope :less_than_a_year, labmda { where("deadline < ", 1.year.ago)}
end
Upvotes: 0
Reputation: 1965
According to rails 3.2 guides
scope :less_than_a_year, lambda { where("deadline < ?", 1.year.from_now ) }
Upvotes: 2