2012-04-30 11 views
0

私は3つのモデル、すなわち賃貸人、部署、基準を持つアプリケーションを持っています。多対多リレーションシップのスコープの作成

# 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 

私は私が部門別やクリテリウムによって賃借人を見つけるようになる賃借人のモデルに2つのスコープを作成しようとしています。独自に動作します

Renter.from_department("SCS") 

または

Renter.has_criterium("Economic considerations") 
を使用して

# 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 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 

私が間違ってやっている上の任意のアイデアを:しかし、私はチェーンにそれらをしようとすると、

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

私が言うエラーが出ますか?

答えて

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.nameまたはdepartments.nameを使用するか、適切なname列を持つテーブルを使用します。

+0

は 'スコープ変更:has_criterium、ラムダ{|クリテリウム| join(:criteria).where( "name =?"、criterium)} 'to 'スコープ:has_criterium、lambda {| criterium | joins(:criteria).where( "criteria.name =?"、criterium)} 'それを修正しました。ありがとう! – Vidur