Question

I'm trying to do something like this:

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

And it's not working....

So, I need to get all my companies that have an id_category and that they have at least one subsidiary within the 'zone'.

I guess this is not the way... please HELP ! :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
Was it helpful?

Solution

You have to "tell" the where clause that the zone is in the subsidiary table:

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

If it says "relation subsidiary was not found", try with:

Company.where(id_category: filter_categorys).joins(:subsidiary).where(tbl_subsidiaries: { zone: 'Nuñez' }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top