Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在自定义模块中使用模型作用域?_Ruby On Rails_Activerecord_Scope - Fatal编程技术网

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>