Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.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 从3升级到4时的Rails范围_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 4_Scope - Fatal编程技术网

Ruby on rails 从3升级到4时的Rails范围

Ruby on rails 从3升级到4时的Rails范围,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,scope,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,Scope,从Rails 3升级到Rails 4时,我遇到了一个“ActiveRecord关联上范围块和不推荐的查找器选项的无效组合”错误。关于这个问题,有几篇文章,其中一些文章使用如下格式: has_many :readable_exams, -> { where trashed_at: nil }, through: :student 然而,当我使用这种格式时,我得到了无效的错误。另一方面,当我这样做时: has_many :readable_exams,

从Rails 3升级到Rails 4时,我遇到了一个“ActiveRecord关联上范围块和不推荐的查找器选项的无效组合”错误。关于这个问题,有几篇文章,其中一些文章使用如下格式:

has_many :readable_exams,
         -> { where trashed_at: nil },
         through: :student
然而,当我使用这种格式时,我得到了无效的错误。另一方面,当我这样做时:

has_many :readable_exams,
         -> { where(trashed_at: nil)
              .through(:student) }
不会抛出任何错误。有人能解释为什么在第一种情况下through会抛出错误,而在第二种情况下却不会

编辑: 事实证明,我的错误来自于一个with_options块,它包装了我以前忽略的has_多

with_options source: :exams,
             primary_key: "student_id",
             group: "exams.id" do |user| # This was throwing the error
    user.has_many :readable_exams,
                  -> { where trashed_at: nil },
                  through: :student
    ...

选项不是在范围块中调用的函数。你需要说:

有很多考试,>{where(trashed_at:nil)},通过::student

关联现在采用可选的第二个参数,即块,它可以为关联指定某些约束(因此是范围块)。您可以在指南中找到哪些方法是有效的:第4.3.3节介绍了
有许多关联的有效方法


需要记住的一点是,在scope块中调用的所有方法都是
ActiveRecord
查询接口的一部分,后面的所有内容都是关联的选项。

感谢您告诉我为什么第二个方法没有抛出错误!不过,我仍然不明白为什么第一种格式(与您的格式相同)会给我一个错误。当我通过删除时,错误消失了。有什么想法吗?