2012-04-30 57 views
0

我有一個應用程序有3個模型:Renters,Departments,Criteria。爲多對多關係編寫範圍

# app/models/department.rb 

class Department < ActiveRecord::Base 
    attr_accessible :name, :abbr 

    has_many :renter_departments 
    has_many :renters, :through => :renter_departments 
end 

# app/models/criterium.rb 

class Criterium < ActiveRecord::Base 
    attr_accessible :name 

    has_many :renter_criteria 
    has_many :renters, :through => :renter_criteria 
end 

我試圖使承租人模式,將讓我按部門和繞圈找租房兩個作用域。

這裏是我的代碼:

# app/models/renter.rb 

class Renter < ActiveRecord::Base 

    # Relationships 
    has_many :renter_departments 
    has_many :renter_criteria 
    has_many :departments, :through => :renter_departments 
    has_many :criteria, :through => :renter_criteria 

    # Scopes 
    scope :from_department, lambda { |abbr| joins(:departments).where("abbr = ?", abbr) } 
    scope :has_criterium, lambda { |criterium| joins(:criteria).where("name = ?", criterium) } 

end 

利用自身

Renter.from_department("SCS") 

Renter.has_criterium("Economic considerations") 

作品。然而,當我嘗試鏈條他們,

Renter.from_department("SCS").has_criterium("Economic considerations") 

我得到一個錯誤,指出:

Renter Load (0.3ms) SELECT "renters".* FROM "renters" INNER JOIN "renter_departments" ON "renter_departments"."renter_id" = "renters"."id" INNER JOIN "departments" ON "departments"."id" = "renter_departments"."department_id" INNER JOIN "renter_criteria" ON "renter_criteria"."renter_id" = "renters"."id" INNER JOIN "criteria" ON "criteria"."id" = "renter_criteria"."criterium_id" WHERE (abbr = 'SCS') AND (name = 'Economic considerations') 
SQLite3::SQLException: ambiguous column name: name: SELECT "renters".* FROM "renters" INNER JOIN "renter_departments" ON "renter_departments"."renter_id" = "renters"."id" INNER JOIN "departments" ON "departments"."id" = "renter_departments"."department_id" INNER JOIN "renter_criteria" ON "renter_criteria"."renter_id" = "renters"."id" INNER JOIN "criteria" ON "criteria"."id" = "renter_criteria"."criterium_id" WHERE (abbr = 'SCS') AND (name = 'Economic considerations') 
ActiveRecord::StatementInvalid: SQLite3::SQLException: ambiguous column name: name: SELECT "renters".* FROM "renters" INNER JOIN "renter_departments" ON "renter_departments"."renter_id" = "renters"."id" INNER JOIN "departments" ON "departments"."id" = "renter_departments"."department_id" INNER JOIN "renter_criteria" ON "renter_criteria"."renter_id" = "renters"."id" INNER JOIN "criteria" ON "criteria"."id" = "renter_criteria"."criterium_id" WHERE (abbr = 'SCS') AND (name = 'Economic considerations') 

此外,

>> Renter.from_department("SCS").class 
=> ActiveRecord::Relation 
>> Renter.has_criterium("Economic considerations").class 
=> ActiveRecord::Relation 

什麼我做錯了任何想法?

回答

1

你需要在你的範圍引用表名:

scope :from_department, lambda { |abbr| joins(:departments).where("departments.abbr = ?", abbr) } 
scope :has_criterium, lambda { |criterium| joins(:criteria).where("criteria.name = ?", criterium) } 
2

您加入的表格中有多個name列。使WHERE子句使用renters.namedepartments.name或具有相應的name列的任何一個表。

+0

更改'範圍:has_criterium,拉姆達{|繞圈|將(:criteria).where(「name =?」,Criterium)}加到'scope:has_criterium,lambda {| criterium | join(:criteria).where(「criteria.name =?」,Criterium)}'並且解決它。謝謝! – Vidur