1

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

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

而且它不工作....

所以,我需要让我所有的公司都有一个 id_category 并且他们在“区域”内至少有一个子公司。

我想这不是办法......请帮助!:D

公司.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

子公司.rb:

class Subsidiary < ActiveRecord::Base

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

end
4

1 回答 1

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' }
于 2013-06-04T20:22:25.160 回答