Question

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

Was it helpful?

Solution

The problem is with this line:

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

This isn't returning records, it's returning an ActiveRecord::Relation, which just means that the query hasn't been evaluated yet. (It does this so you can chain together filters and it only gets evaluated to SQL when you iterate over it.

Without knowing what your Day model looks like, I think what you want is this:

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

This will return the first result, or nil if there were no results. Then if today is non nil, it will have the reminders method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top