user2186901
user2186901

Reputation: 13

Rails 3.2 Model scope date less_than_a_

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

Answers (3)

mraaroncruz
mraaroncruz

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

Naveed
Naveed

Reputation: 11167

class Goal < ActiveRecord::Base

 #need help solving this
 scope :less_than_a_year, labmda { where("deadline < ", 1.year.ago)}

end

Upvotes: 0

Yuri Golobokov
Yuri Golobokov

Reputation: 1965

According to rails 3.2 guides

scope :less_than_a_year, lambda { where("deadline < ?", 1.year.from_now ) }

Upvotes: 2

Related Questions