bnussey
bnussey

Reputation: 1964

Query rails with condition and order with a limit of records returned

I am trying to perform a query on rails where I return 10 products from a specific category with the highest Status.

I've come up with the query below which isn't returning anything. I've tried many variations of it but can't seem to get this working. Seems very basic so I'm quite frustrated. Can anyone assist?

Product.where("category_id = 4").limit(10).order("status desc") do |a|

    puts a.name

end

Upvotes: 0

Views: 67

Answers (1)

usha
usha

Reputation: 29349

Try this

Product.where("category_id = 4").order("status desc").limit(10).each do |a|
    puts a.name
end

or this

p Product.where("category_id = 4").order("status desc").limit(10).collect(&:name)

Upvotes: 1

Related Questions