2013-03-06 55 views
0

組織類看起來是這樣的:添加JOIN的相關表格

has_many Students 

學生類看起來是這樣的:

has_many Klasses 
belongs_to Organization 

克拉斯類看起來是這樣的:

some field named : price 
scope :top_expensive_classes, joins(:students).order('price DESC') 
belongs_to Student 

而且我的查詢是這樣的:

@results = Klass.top_expensive_classes.where(organization_id: params[:id]).limit(RESULT_SET_COUNT) 

注意,它開始與克拉斯,所以這就是問題所在,因爲我在那裏類的organization_ID但不是在克拉斯搜索,它是學生類,所以不知何故,我應該介紹一個連接到某個地方解決這個問題,但無法弄清楚。

回答

1

我認爲真正的問題是您的關聯可能不正確。

  • 的學生有很多類
  • 類有很多學生

,但你有什麼是

  • 的學生有很多類
  • 類屬於單個學生

這並沒有什麼意義(至少在我見過課堂和學生互動的任何情況下)。您應該創建多對多關係,而不是KlassStudent之間的一對多關係。

class Student < ActiveRecord::Base 
    has_many :klasses, through: :student_klasses 
    has_many :student_klasses 
end 

class Klass < ActiveRecord::Base 
    has_many :students, through: :student_klasses 
    has_many :student_klasses 
end 

class StudentKlass < ActiveRecord::Base 
    belongs_to :student 
    belongs_to :klass 
end 

一旦你有了這些適當的正確的組織,你需要調用.joinsKlass:students關聯。你可以沒有範圍。

Klass.joins(:students).where("students.organization_id = ?", params[:id]).order('price DESC').limit(RESULT_SET_COUNT) 

在ActiveRecord上閱讀the guide查詢。


這裏是證明(使用上面的確切型號定義)該協會的排序確實問題。

irb(main):001:0> s = Student.create(name: "Deefour") 
    SQL (3.6ms) INSERT INTO "students" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Fri, 08 Mar 2013 01:33:32 UTC +00:00], ["name", "Deefour"], ["updated_at", Fri, 08 Mar 2013 01:33:32 UTC +00:00]] 
=> #<Student id: 1, name: "Deefour", created_at: "2013-03-08 01:33:32", updated_at: "2013-03-08 01:33:32"> 
irb(main):002:0> kk = [] 
=> [] 

irb(main):003:0> kk << Klass.create(title: "Klass 1") 
    SQL (0.3ms) INSERT INTO "klasses" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Fri, 08 Mar 2013 01:34:06 UTC +00:00], ["title", "Klass 1"], ["updated_at", Fri, 08 Mar 2013 01:34:06 UTC +00:00]] 
=> [#<Klass id: 1, title: "Klass 1", created_at: "2013-03-08 01:34:06", updated_at: "2013-03-08 01:34:06">] 

irb(main):004:0> kk << Klass.create(title: "Klass 2") 
    SQL (0.3ms) INSERT INTO "klasses" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Fri, 08 Mar 2013 01:34:14 UTC +00:00], ["title", "Klass 2"], ["updated_at", Fri, 08 Mar 2013 01:34:14 UTC +00:00]] 
=> [#<Klass id: 1, title: "Klass 1", created_at: "2013-03-08 01:34:06", updated_at: "2013-03-08 01:34:06">, #<Klass id: 2, title: "Klass 2", created_at: "2013-03-08 01:34:14", updated_at: "2013-03-08 01:34:14">] 

irb(main):005:0> s.klasses = kk 
    Klass Load (0.1ms) SELECT "klasses".* FROM "klasses" INNER JOIN "student_klasses" ON "klasses"."id" = "student_klasses"."klass_id" WHERE "student_klasses"."student_id" = ? [["student_id", 1]] 
    SQL (0.4ms) INSERT INTO "student_klasses" ("created_at", "klass_id", "student_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Fri, 08 Mar 2013 01:34:29 UTC +00:00], ["klass_id", 1], ["student_id", 1], ["updated_at", Fri, 08 Mar 2013 01:34:29 UTC +00:00]] 
    SQL (0.1ms) INSERT INTO "student_klasses" ("created_at", "klass_id", "student_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Fri, 08 Mar 2013 01:34:29 UTC +00:00], ["klass_id", 2], ["student_id", 1], ["updated_at", Fri, 08 Mar 2013 01:34:29 UTC +00:00]] 
=> [#<Klass id: 1, title: "Klass 1", created_at: "2013-03-08 01:34:06", updated_at: "2013-03-08 01:34:06">, #<Klass id: 2, title: "Klass 2", created_at: "2013-03-08 01:34:14", updated_at: "2013-03-08 01:34:14">] 

irb(main):006:0> Student.first.klasses.map(&:id) 
    Student Load (0.2ms) SELECT "students".* FROM "students" ORDER BY "students"."id" ASC LIMIT 1 
    Klass Load (0.1ms) SELECT "klasses".* FROM "klasses" INNER JOIN "student_klasses" ON "klasses"."id" = "student_klasses"."klass_id" WHERE "student_klasses"."student_id" = ? [["student_id", 1]] 
=> [1, 2] 
+0

我是否還需要保持我在該範圍內的加入?目前它說它找不到學生表。我們在某處是否有語法錯誤?也許應該是學生而不是學生? – Bohn 2013-03-06 21:08:32

+0

它說「沒有找到名爲'學生'的協會」 – Bohn 2013-03-06 21:18:16

+1

它看起來像你的協會不完全是他們應該的。我已經更新了我的答案,以幫助指導您。 – deefour 2013-03-06 21:47:54