Skip to content Skip to sidebar Skip to footer

Rails Nested Joins Activerecord With Conditions

I'm trying to write a nested joins query with a condition. The query I have right now is: Event.joins(:store => :retailer).where(store: {retailer: {id: 2}}) Which outputs the f

Solution 1:

Looks like you don't need nested joins here. Try to use something like

Event.joins(:store).where(stores: {retailer_id: 2})

Nested join should also work using stores

Event.joins(:store => :retailer).where(stores: {retailer: {id: 2}})

Solution 2:

There is the simplest way instead of using curly brackets :

Event.joins(:store => :retailer).where('stores.retailer_id => ?', 2)

Solution 3:

You should be able to access the retailers id with the following syntax:

Event.joins(:store => :retailer).where('retailers.id' => 2)

ps. tested on Rails 5

Post a Comment for "Rails Nested Joins Activerecord With Conditions"