链接wheres加入ActiveRecord

时间:2013-06-04 20:08:31

标签: ruby-on-rails ruby join where chain

我正在尝试做这样的事情:

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

它没有用......

所以,我需要让我所有拥有id_category的公司,并且他们在'zone'中至少有一个子公司。

我想这不是方法......请帮助! :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

1 个答案:

答案 0 :(得分: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' }