Ruby on rails 如何在自定义模块中使用模型作用域?
在我的模型中,我定义了范围:Ruby on rails 如何在自定义模块中使用模型作用域?,ruby-on-rails,activerecord,scope,Ruby On Rails,Activerecord,Scope,在我的模型中,我定义了范围: class MyModel < ActiveRecord::Base scope :accessible_for, ->(user) { where(user_id: user.id) } end 不幸的是,我在调用do_export后收到一个错误 undefined method `accessible_for' for <Class:0x000000065396d8> 那么,如何正确使用它呢?在另一台工作站上成功运行此代码后,我重
class MyModel < ActiveRecord::Base
scope :accessible_for, ->(user) { where(user_id: user.id) }
end
不幸的是,我在调用do_export后收到一个错误
undefined method `accessible_for' for <Class:0x000000065396d8>
那么,如何正确使用它呢?在另一台工作站上成功运行此代码后,我重新启动了计算机,一切正常。因此,代码中没有任何错误。您的范围被命名为Accessible_,用于使用单个s。你打错了!哦,这只是一个有问题的打字错误!定义为Scope:accessible\u for、->user{whereuser\u id:user.id}的Scope也可以正常工作,当我从Rails控制台调用do\u export时。。。
module Reports
class ReportMyModel
def do_export(user)
to_export = MyModel.accessible_for user
end
end
end
undefined method `accessible_for' for <Class:0x000000065396d8>