I have these 3 models:
class Car < ActiveRecord::Base
belongs_to :user
has_many :car_styles
has_many :styles, :through => :car_styles
end
class CarStyle < ActiveRecord::Base
belongs_to :car
belongs_to :style
end
class UserStyle < ActiveRecord::Base
belongs_to :style
belongs_to :user
end
class User < ActiveRecord::Base
has_many :cars
has_many :car_styles
has_many :styles, :through => :car_styles
end
I have logged-in a user (current_user). I am trying to select all cars, where car style_id are the same like user style_id — how can I do that?
Thanks for help in advance
EDIT: schema:
cars
-id
-user_id
car_styles
-car_id
-style_id
user_styles
-user_id
-style_id
Every user has saved favorite styles – doesn’t matter how much (but approximately ±5). Every photo has added a styles as well – and again doesn’t matter how much.
And I would like to select all cars from table Cars, which have the same styles, as the current_user has added.
I think what you’re saying here is you have three main models:
Car, User and Style
CarStyle and UserStyle are joins right?
If so, you should be able to say:
If not, could you update the question with your schema?