IT_puppet_master
IT_puppet_master

Reputation: 702

Filter/scope by child records with has_and_belongs_to_many relationship

I have a model Service with has_and_belongs_to_manyrelationship to a Languages model.

I would like to get a scope of all the services that have a given language.

I have tried Service.joins(:languages).where(:languages => Language.find(1)) but get Mysql2::Error: Unknown column 'services.language_id'as its using a 3rd table to create the association.

Now im stuck, any pointers please wonderful people?

Upvotes: 0

Views: 82

Answers (1)

Pradeep Sapkota
Pradeep Sapkota

Reputation: 2072

This must be the answer

@language = Language.find_by_id(1)
@services = @language.services

Then run loop.

Upvotes: 1

Related Questions