3

I am trying to set up a custom rake task for a Rails app that checks to see if your location is within a certain area with street cleaning restrictions. If it is, you can click a button "Remind Me" that will save the user id and restriction id to a reminder table.

I have the Rufus gem running the rake task every 15 minutes.

What I then want is the rake task to check the current time and day with the day and start time that corresponds with the restriction id, and if it is within 30 minutes, it should email the user to move their car.

When I run my current code below, I get this error:

rake aborted!
undefined method `reminders' for # <ActiveRecord::Relation::ActiveRecord_Relation_Day:0x007fb38c3274f8>
/Users/m/WDI/park_pointer/lib/tasks/reminder.rake:11:in `block (2 levels) in <top (required)>'
Tasks: TOP => reminder:mail_users

Here's my entire project: https://github.com/mnicole/park_pointer

But the code for the rake task is:

namespace :reminder do
desc "REMINDER"
task :mail_users => :environment do


time = Time.new
t = Time.now
today = Day.where(:id => time.wday + 1) #returns 0 for sunday, etc

@reminders = today.reminders

@reminders.each do |reminder|
@reminder = reminder

  reminder_time_in_mins = restriction.start_time.hour * 60
  current_time_in_mins = (Time.now.hour * 60) + Time.now.min
  # last_time_in_mins = record.last.hour * 60 + record.last.min

if current_time_in_mins > (reminder_time_in_mins - 30)
  @user = User.find(reminder.user_id.last)
  UserMailer.reminder_email(user_id).deliver!
end

end
end
end

I THINK I'm almost there, but I've been working on this for a few months and am kind of at a loss about the next step.

Thanks in advance!!

-Michele

4

1 回答 1

1

问题在于这一行:

today = Day.where(:id => time.wday + 1)

这不是返回记录,而是返回一个ActiveRecord::Relation,这仅表示尚未对查询进行评估。(这样做是为了让您可以将过滤器链接在一起,并且只有在您对其进行迭代时才会将其评估为 SQL。

在不知道您的Day模型是什么样子的情况下,我认为您想要的是:

today = Day.where(:id => time.wday + 1).first

这将返回第一个结果,或者nil如果没有结果。然后如果today是非零,它就会有reminders方法。

于 2014-04-12T20:11:29.213 回答