1

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.

4

1 回答 1

2
@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.

于 2013-08-03T12:37:03.290 回答