Andrey Drozdov
Andrey Drozdov

Reputation: 591

Select from DB without some value

I have my own rails app and I need to specify what I want to get from DB (pg). Ordinary, we do it like that: @posts = Post.all and I will get all the post in my Post Table.

In Post Table I have a categories, as a boolean: (photo, video, etc.) and when I want to select only posts witch response to photo category, we do it like that: @posts = Post.where(photo: true)

But what I need, is to select all posts without, for example, video. It have to look something like that: @posts = Post.all.without(video:true). How to make it happen?

UPDATED

And is there any possibility to block several values? I mean something like that Post.where.not(id: 1 && 2). I really don't know how it must look like. What i need - is to select all Posts without posts with ID 1 and 2.

Upvotes: 0

Views: 71

Answers (3)

eweb
eweb

Reputation: 749

You can pass an array of values: Post.where.not(id: [1, 2])

Upvotes: 1

Bartłomiej Gładys
Bartłomiej Gładys

Reputation: 4615

you can use .not, for example

@posts = Post.where.not(video:true)

Upvotes: 1

Ursus
Ursus

Reputation: 30056

Try

@posts = Post.where.not(video: true)

Upvotes: 5

Related Questions