Reputation: 3333
I have the following models:
class Distributor < ActiveRecord::Base
has_many :products
end
class Producer < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
has_one :favorite
belongs_to :producer
belongs_to :distributor
end
class Favorite < ActiveRecord::Base
belongs_to :product
end
class User < ActiveRecord::Base
has_many :favorites
end
I would like to build a AR expression is analog of sql query:
select *
from `favorites`
inner join `products` on `products`.`id` = `favorites`.`product_id`
inner join `producers` on `producers`.`id` = `products`.`producer_id`
inner join `distributors` on `distributors`.`id` = `products`.`distributor_id`
where `favorites`.`user_id` = 1
Upvotes: 0
Views: 34
Reputation: 15089
You can use a nested set of joins
methods like this:
Favorite.joins(:product => [:producer , :distributor]).where("favorites.user_id = 1")
Note that i am using the =>
notation, but you can use the ruby 1.9+ one too.
Upvotes: 1