【问题标题】:How to get the results of other tables using includes or joins in Ruby on Rails如何在 Ruby on Rails 中使用包含或连接获取其他表的结果
【发布时间】:2017-03-07 14:21:45
【问题描述】:

我想查询 private_classes 的结果。场景是:

class PrivateClass < ActiveRecord::Base
  belongs_to :private_school
  has_many :lesson_plans
end

class JoinedPrivateSchool < ActiveRecord::Base
  belongs_to :student
  belongs_to :private_school
end

class PrivateSchool < ActiveRecord::Base
  belongs_to :teacher
  has_many :private_classes
  has_many :joined_private_schools
end

class Student < ActiveRecord::Base
  has_many :joined_private_schools
end

JoinPrivateSchool 具有属性private_school_id

我在做:

s = Student.find(9)

s.joined_private_schools

导致:

=> #<ActiveRecord::Associations::CollectionProxy [#<JoinedPrivateSchool id: 8,
 user_id: 73, student_id: 9, private_school_id: 28, created_at: "2017-02-16 
12:38:37", updated_at: "2017-02-16 12:38:37">, #<JoinedPrivateSchool id: 9,
 user_id: 73, student_id: 9, private_school_id: 33, created_at: "2017-02-16 
12:42:01", updated_at: "2017-02-16 12:42:01">, #<JoinedPrivateSchool id: 12, 
user_id: 73, student_id: 9, private_school_id: 32, created_at: "2017-02-16 
13:19:02", updated_at: "2017-02-16 13:19:02">]>

如果我这样做:

c = s.joined_private_schools.includes(private_school: :private_classes)

结果:

JoinedPrivateSchool Load (0.6ms)  SELECT "joined_private_schools".* FROM 
"joined_private_schools" WHERE "joined_private_schools"."student_id" = $1 
 [["student_id", 9]]


PrivateSchool Load (0.9ms)  SELECT "private_schools".* FROM "private_schools" 
WHERE "private_schools"."id" IN (28, 33, 32)  ORDER BY 
"private_schools"."created_at" DESC


PrivateClass Load (0.7ms)  SELECT "private_classes".* FROM "private_classes" 
WHERE "private_classes"."private_school_id" IN (33, 32, 28)  ORDER BY 
"private_classes"."created_at" DESC


=> #<ActiveRecord::AssociationRelation [#<JoinedPrivateSchool id: 8, user_id: 
73, student_id: 9, private_school_id: 28, created_at: "2017-02-16 12:38:37",
 updated_at: "2017-02-16 12:38:37">, #<JoinedPrivateSchool id: 9, user_id: 73,
 student_id: 9, private_school_id: 33, created_at: "2017-02-16 12:42:01", 
updated_at: "2017-02-16 12:42:01">, #<JoinedPrivateSchool id: 12, user_id: 73,
 student_id: 9, private_school_id: 32, created_at: "2017-02-16 13:19:02", 
updated_at: "2017-02-16 13:19:02">]> 

但这仍然是错误的结果。

我需要使用一个查询来获取 private_classes 的结果,这样我就可以避免多个 each 循环。

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-4 rails-activerecord


【解决方案1】:

如果你想拥有PrivateClass,你就从错误的地方开始(User)。你需要从PrivateClass开始加入所有的中间表并使用用户id作为条件:

PrivateClass.joins(private_school: :user).where(user: {id: 9})

# or a little more performant (avoids one join)

PrivateClass.joins(:private_school).where(private_school: {user_id: 9})

【讨论】:

    【解决方案2】:

    您可以在关联上使用:through(至少在 Rails 4.2 中)
    我更新了这个,我第一次错过了你想要 private_classes

    class JoinedPrivateSchool < ActiveRecord::Base
      belongs_to :student
      belongs_to :private_school
      has_many :private_classes, through: :private_schools
    end
    
    class Student < ActiveRecord::Base
      has_many :joined_private_schools
      has_many :private_classes, through: :joined_private_schools
    end
    
    s = Student.find(9)
    s.private_classes
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 2020-06-24
      • 1970-01-01
      • 2011-05-10
      相关资源
      最近更新 更多