Raza
Raza

Reputation: 2390

where clause with includes

I have a User model which has has_many relationship with Identity model. I want to execute includes query with where clause.

User.includes(:identities).where("identities.provider = 'facebook')

But it is giving me error.

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "identities"

while it works fine with joins

User.joins(:identities).where("identities.provider = 'facebook')

I don't want to use scope. I just want to use where like I do with joins clause.

Upvotes: 0

Views: 81

Answers (1)

Pavan
Pavan

Reputation: 33542

From APIdoc

If you want to add conditions to your included models you’ll have to explicitly reference them

When using includes with conditions, you need add .references at the end. The below should work

User.includes(:identities).where('identities.provider = ?', 'facebook').references(:identities)

Upvotes: 2

Related Questions