Maki
Maki

Reputation: 913

rails 3 not nil

I'm trying to select all records that are not null from my table using where method

MyModel.where(:some_id => !nil) 

but it doesn't work, is there any other solution to do this?

Upvotes: 8

Views: 4668

Answers (3)

Benoit Garret
Benoit Garret

Reputation: 13675

You can do it using the Arel syntax (which has the bonus of being database independent):

MyModel.where(MyModel.arel_table['some_id'].not_eq(nil))

Upvotes: 12

Rob Di Marco
Rob Di Marco

Reputation: 44982

Use a string rather than a hash

MyModel.where("some_id is not null")

Upvotes: 7

Matteo Alessani
Matteo Alessani

Reputation: 10422

You can use:

MyModel.where("some_id IS NOT NULL") 

Upvotes: 2

Related Questions