Rajdeep Singh
Rajdeep Singh

Reputation: 17834

SQL query to ActiveRecord query

How to convert this into an active record query?

select quantity from carts where cart_id = ? and product_id = ?

Upvotes: 1

Views: 86

Answers (2)

David Aldridge
David Aldridge

Reputation: 52346

If you're interested only in the value, and not the overhead of the object (so you're not interested in any of the Cart methods, associations etc), then pluck is available and a little better performing:

Cart.pluck(:quantity).where(:cart_id => cart_id, :product_id => product_id)

Upvotes: 1

Ryan Bigg
Ryan Bigg

Reputation: 107728

Cart.select(:quantity).where(:cart_id => cart_id, :product_id => product_id)

Upvotes: 1

Related Questions