开发者

Task.find Conditions

开发者 https://www.devze.com 2023-01-15 20:48 出处:网络
I\'m building my first Rails project - its a ToDo app, which are supposed to send out warnings when a Task are X minutes from its deadline.

I'm building my first Rails project - its a ToDo app, which are supposed to send out warnings when a Task are X minutes from its deadline.

Im trying to create a variabel with the tasks that are X minutes from its deadline. The X minutes are diffrent for every Task - which means that its stored in the database. So I got two columns in the database.

开发者_运维知识库1: Task.close_time => A Time column with the actual deadline of the Task.

2: Task.close_time_warning => A integer column with how many minutes the warning should be sent out before the deadlone.

I then have the following code, but I get errors trying to load the rss feed whit the warnings.

def warnings

@warnings = Task.find(:all, :conditions => ["completed_at = ? and state_active = ? and close_time < ?", nil, true, Date.new(2000, 1, 1).to_datetime + Time.now.seconds_since_midnight.seconds + task.close_time_warning.minutes])

respond_to do |format|
  format.rss
end

end


You'll probably need to use a database function to subtract the times. In mysql it is date_sub for example.

It may be more efficient, however, to store a derived field notify_time which you set to the correct time to notify. This will make your query for tasks to notify for more efficient. You could set that value using a callback, something like:

class Task < ActiveRecord::Base
  def before_save
    if close_time && close_time_warning
      notify_time = close_time - close_time_warning.minutes
    end
  end
end

Then your test for tasks to notify for is:

@warnings = Task.find(:all, 
    :conditions => ['completed_at is not null and notify_time > ?', Time.now]
0

精彩评论

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