Rails 5.2 中作用域中的连接与 rails 5.1 有何不同



在我将rails从5.1升级到5.2后,我开始收到以下错误:

NoMethodError: undefined method `expr' for nil:NilClass
  from /gems_path/activerecord-5.2.0/lib/active_record/associations/join_dependency/join_association.rb:47:in `block in join_constraints'
  from /gems_path/activerecord-5.2.0/lib/active_record/associations/join_dependency/join_association.rb:33:in `reverse_each'
  from /gems_path/activerecord-5.2.0/lib/active_record/associations/join_dependency/join_association.rb:33:in `join_constraints'
  from /gems_path/activerecord-5.2.0/lib/active_record/associations/join_dependency.rb:167:in `make_constraints'
  from /gems_path/activerecord-5.2.0/lib/active_record/associations/join_dependency.rb:177:in `make_join_constraints'
  from /gems_path/activerecord-5.2.0/lib/active_record/associations/join_dependency.rb:104:in `block in join_constraints'
  from /gems_path/activerecord-5.2.0/lib/active_record/associations/join_dependency.rb:103:in `each'
  from /gems_path/activerecord-5.2.0/lib/active_record/associations/join_dependency.rb:103:in `flat_map'
  from /gems_path/activerecord-5.2.0/lib/active_record/associations/join_dependency.rb:103:in `join_constraints'
  from /gems_path/activerecord-5.2.0/lib/active_record/relation/query_methods.rb:1026:in `build_join_query'
  from /gems_path/activerecord-5.2.0/lib/active_record/relation/query_methods.rb:1008:in `build_joins'
  from /gems_path/activerecord-5.2.0/lib/active_record/relation/query_methods.rb:928:in `build_arel'
  from /gems_path/activerecord-5.2.0/lib/active_record/relation/query_methods.rb:903:in `arel'
  from /gems_path/activerecord-5.2.0/lib/active_record/relation.rb:554:in `block in exec_queries'
  from /gems_path/activerecord-5.2.0/lib/active_record/relation.rb:578:in `skip_query_cache_if_necessary'
  from /gems_path/activerecord-5.2.0/lib/active_record/relation.rb:542:in `exec_queries'
  from /gems_path/activerecord-5.2.0/lib/active_record/relation.rb:414:in `load'
  from /gems_path/activerecord-5.2.0/lib/active_record/relation.rb:200:in `records'
  from /gems_path/activerecord-5.2.0/lib/active_record/relation/delegation.rb:41:in `[]'

导致错误的代码如下所示:

class Post
  has_many :comments
  has_one :last_comment, -> {
    joins("LEFT JOIN posts on posts.id = comments.post_id")
    .where("
      comments.id = (
        SELECT MAX(comments.id) FROM comments
        WHERE comments.post_id = posts.id
      )"
    )
  }, class_name: "Comment"
  scope :with_last_comment, -> { joins(:last_comment) }
end

我创建了这个要点,其中包含有助于重现错误的完整代码。将issue_with_joins_in_scopes_in_rails_5_3.rb下载到您的 PC 并运行它

ruby issue_with_joins_in_scopes_in_rails_5_3.rb

您可以查看此 Github 问题以获取更多详细信息

Rails 5.2

和 5.1 中的连接之间有什么区别,它们会导致代码Post.with_last_comment在 Rails 5.2 中失败并出错?

如何更改 Post 模型中last_comment关联和with_last_comment范围,使其在 Rails 5.2 中正常工作?

发布有关如何为帖子集合预加载最后评论的部分解决方案

首先,您需要具有以下last_comment关联:

class Post < ApplicationRecord
  has_many :comments, dependent: :destroy
  has_one :last_comment, -> { order(id: :desc) }, class_name: "Comment"
end

它适用于 Rails 5.2 中的 preloadincludes,并生成以下 SQL 查询:

Post.includes(:last_comment)
Post Load (0.2ms)  SELECT  "posts".* FROM "posts" LIMIT ?  [["LIMIT", 11]]
Comment Load (0.2ms)  SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? ORDER BY "comments"."id" DESC  [["post_id", 1]]

此解决方案的问题在于,当我使用joins时,它会忽略关联范围并生成以下SQL查询:

Post.joins(:last_comment)
Post Load (0.2ms)  SELECT  "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" LIMIT ?  [["LIMIT", 11]]

我能够通过以下方式解决它更改原始last_comment关联:

class Post < ApplicationRecord
  has_many :comments, dependent: :destroy
  has_one :last_comment, -> {
    joins(:post)                               # <--- Note this change
    .where("
      comments.id = (
        SELECT MAX(comments.id) FROM comments
        WHERE comments.post_id = posts.id
      )"
    )
  }, class_name: "Comment"
  scope :with_last_comment, -> { joins(:last_comment) }
end

现在Post.with_last_comment生成以下 SQL:

Post Load (0.3ms)  SELECT  "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" INNER JOIN "posts" "posts_comments" ON "posts_comments"."id" = "comments"."post_id" AND (
      comments.id = (
        SELECT MAX(comments.id) FROM comments
        WHERE comments.post_id = posts.id
      )) LIMIT ?  [["LIMIT", 11]]

关于 Rails 5.2 中的连接与 Rails 5.1 有何不同的问题仍然悬而未决。

最新更新