我想查询一个使用连接来查找需要警报的用户的数据库。我的查询在它为其编写的模型中正常工作,并且在传递的参数是显式的时它也可以在范围内工作。但是,当我将参数作为变量传递时,它不起作用。这里是我的代码:在Rails 3中传递的跨作用域参数
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
我得到的错误是一个NameError未定义的局部变量或方法。但我会想在滑轨控制台中传递变量的值,例如:
User.alerts_to_be_sent(5)
会好的。帮助,将不胜感激。
谢谢你救了我很多挫折。我根本不需要使用范围。组合的类方法很好地工作。 – Hugs 2012-02-08 22:10:45