byCoder
byCoder

Reputation: 9184

Rails fetch with limit order and "not"

In my app i use rails 4.

And i need to fetch as i do now data, but all except one params[:id], now i have such code:

@vip_cars = Car.order(created_at: :desc, is_vip: :desc).limit(100).sample(4)

and i try something like:

@vip_cars = Car.order(created_at: :desc, is_vip: :desc).not(id: params[:id]).limit(100).sample(4)

but it didn't work. What i do wrong? how to fetch data with order, limit, sample and not in rails 4?

Upvotes: 2

Views: 169

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118271

You should write as

Car.order(created_at: :desc, is_vip: :desc)
   .where.not(id: params[:id])
   .limit(100).sample(4)

Read #not

Returns a new relation expressing WHERE + NOT condition according to the conditions in the arguments.

Remember #not always work in conjunction with #where.

Upvotes: 5

Related Questions