In my application I have models: Car:
class Car < ActiveRecord::Base
has_one :brand, through: :car_configuration
has_one :model, through: :car_configuration
has_one :body_style, through: :car_configuration
has_one :car_class, through: :car_configuration
belongs_to :car_configuration
end
CarConfiguration:
class CarConfiguration < ActiveRecord::Base
belongs_to :model, class_name: 'CarModel'
belongs_to :body_style, class_name: 'CarBodyStyle'
belongs_to :car_class
has_one :brand, through: :model
has_many :cars, dependent: :destroy
has_many :colors, dependent: :destroy
def brand_id
brand.try(:id)
end
end
and CarBrand:
class CarBrand < ActiveRecord::Base
default_scope { order(name: :asc) }
validates :name, presence: true
has_many :models, class_name: 'CarModel', foreign_key: 'brand_id'
end
Now I want to get all Cars which CarConfiguration brand id is for example 1. I tried something like this but this not work:
joins(:car_configuration).where(car_configurations: {brand_id: 1})
Thanks in advance for any help.