I am trying to query a db that uses a join to find users that require an alert. My query works fine in the model it is written for and it also works through the scope when the parameter being passed is explicit. However when I pass the parameter as a variable it does not work. Here is my code:
class Alert < ActiveRecord::Base
belongs_to :user
attr_accessible :first_alert, :second_alert, :third_alert, :user_id
def self.find_users_to_alert(time)
where(:third_alert => time)
end
scope :alerter, find_users_to_alert (here is where i want to pass param)
end
class User < ActiveRecord::Base
has_many :alerts
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
scope :alerts_to_be_sent, joins(:alerts) & Alert.alerter
end
The error I am getting is a NameError undefined local variable or method. But I would have thought passing the value of the variable in the rails console like:
User.alerts_to_be_sent(5)
would be ok. Help would be appreciated thanks.
From the Active Record Query Interface Guide:
So write class methods instead: