Reputation: 881
I have Country, Car, RentalType and CarType models.
Country Model
has_many :cars
Car Model
belongs_to :country
belongs_to :car_group
RentalType Model
has_many :car_rentals
has_many :cars, :through => :car_rentals
has_many :car_group_types
has_many :car_groups, :through => :car_group_types
CarGroup Model
has_many :cars
has_many :car_group_types
has_many :rental_types, :through => :car_group_types
So I would like to get the cars that belongs to the country, where it's rental type name is 'awake' and where it's car_group
name is 'toyota'.
So far I have tried;
b = @country.cars.includes(:rental_types).where(:rental_types => { name: @rental_type.name } ).all
This works fine but when I try to add car group, it gives error;
b = @country.cars.includes(:rental_types).where(:rental_types => { name: @rental_type.name } ).where(:car_group => { name: @car_group.name } ).all
ERROR:
SQLite3::SQLException: no such column: car_group.name: SELECT DISTINCT COUNT(DISTINCT "cars"."id") FROM "cars" LEFT OUTER JOIN "car_rentals" ON "car_rentals"."car_id" = "cars"."id" LEFT OUTER JOIN "rental_types" ON "rental_types"."id" = "car_rentals"."rental_type_id" LEFT OUTER JOIN "car_groups" ON "car_groups"."id" = "cars"."car_group_id" WHERE "cars"."car_country_id" = ? AND "rental_types"."name" = ? AND "car_group"."name" = ?
Upvotes: 0
Views: 21
Reputation: 2624
Add :car_group
to your :includes
:
includes(:rental_types, :car_group)
:car_group
should be plural
noun in your :where
:
where(:car_groups => { name: @car_group.name } )
Upvotes: 1