views:

19

answers:

1

Hi,

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

Help is wanted :)

Kenneth

A: 

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]
Shadwell
Hi.. I also looked into the before_save concept, but seem to be able to get it working.. I'll give it a go again :)
Twiddr
but shouldn't the before_save function be in the model?
Twiddr
sorry.. i was to fast on that last comment.. :)
Twiddr
Yes, sorry, missed off the < ActiveRecord::Base. Will update.
Shadwell
Twiddr
The datatype would need to be `:datetime` rather than `:time`. I've just spotted a typo too in the `notify_time = ` line which I'll fix.
Shadwell
Hmm.. Still doesn't work :)Still nil values..
Twiddr
Gah! Sorry, You need to use `self.notify_time = self.close_time - self.close_time_warning.minutes`
Shadwell
There we go.. :) That helped.. Thanks so much for your help.. I've been on this issue for days now :) Have a nice weekend..
Twiddr
No problem. Glad it worked in the end.
Shadwell