2013-06-04 42 views
1

我試圖做這樣的事情:鏈與哪來的ActiveRecord加入

filter_categorys = params[:filter_categorys] 
companies = Company.where('id_category = (?)', filter_categorys).joins(:subsidiary).where('zone = Nuñez') 

而且它不工作....

所以,我需要讓我的所有公司有一個id_category並且他們在「區域」內至少有一個子公司。

我想這不是...請幫助! :d

Company.rb:

class Company < ActiveRecord::Base 

    has_many :subsidiary, :foreign_key => :id_company 
    has_many :benefit, :foreign_key => :id_company 
    set_primary_key :id_company 
    self.table_name = 'tbl_companys' 

end 

Subsidiary.rb:

class Subsidiary < ActiveRecord::Base 

    belongs_to :company 
    set_primary_key :id_subsidiary 
    self.table_name = 'tbl_subsidiaries' 

end 
+0

使用'id_category IN(?)'(不相等) – MrYoshiji

+0

謝謝,但問題是試圖獲取對tbl_companys的「區域」列及其對tbl_subsidiaries –

回答

2

你必須 「告訴」 where子句,該區域是在子公司表:

Company.where(id_category: filter_categorys).joins(:subsidiary).where(subsidiary: { zone: 'Nuñez' } 

如果它顯示「沒有找到關係子公司」,請嘗試:

Company.where(id_category: filter_categorys).joins(:subsidiary).where(tbl_subsidiaries: { zone: 'Nuñez' } 
+0

第二個答案摸索出恰到好處! 我很感謝MrYoshiji! 感謝:D !!!!! –