开发者

Search date with the condition

I have a model, where define block looks like:

define_index do
  indexes title, content, manager, note, start_date, end_date

  has created_at, updated_a开发者_StackOverflow社区t

  has user_id, :as => :user_id, :type => :integer

  set_property :delta => true
end

I want to search tasks by date, which can fall into the gap between start_date..end_date , how can i do this ?


Seems like a standard Active Record SQL call. Since you're passing it parameters, you can do a hashed query like so:

    tasks = Task.where("start_date > :start_date and end_date < :end_date", {:start_date => start_date, :end_date => end_date})

Drop that in a method that supplies start_date and end_date like so:

def tasks_within_range(start_date, end_date)
  Task.where("start_date > :start_date and end_date < :end_date {:start_date => start_date, :end_date => end_date})
end

and you've got a dynamic finder! :)


You'll want to have start_date and end_date as attributes, not fields:

define_index do
  indexes title, content, manager, note

  has created_at, updated_at, start_date, end_date
  has user_id, :as => :user_id, :type => :integer

  set_property :delta => true
end

And then, you'll use ranges to limit results on those two attributes:

today = Date.today.to_time # or whatever you like
Task.search :with => {
  :start_date => (Time.at(0)..date),
  :end_Date   => (date..Time.at(2 ** 32))
}

That should do the job - or perhaps add a + 1.day to the second range. Tweak as you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜