Andrey Yasinishyn
Andrey Yasinishyn

Reputation: 1861

can't query by association field value

I need to query only Realties where user id confirmed. I am using "devise" gem for authentication, and my query looks like this:

@search = Realty.includes(:user).where("users.confirmed_at != ?", nil)

As result I get => [], but there are many Realties records where user.confirmed? => true. I double checked it from the console.

The association structure looks like this:

class Realty
     belongs_to :user
.....
class User
     has_many :realties
.....

Please help me or tell where I make an mistake ?? Thank you.

Upvotes: 1

Views: 84

Answers (1)

Luís Ramalho
Luís Ramalho

Reputation: 10208

@search = Realty.includes(:user).where("users.confirmed_at IS NOT NULL")

You cannot use != in a SQL statement.

If you want to check if the value is not empty as well, then you can do

@search = Realty.includes(:user).where("users.confirmed_at <> ''")

it will return the results where users.confirmed_at is neither null nor empty.

Upvotes: 2

Related Questions