Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何将其转换为活动记录查询?
select quantity from carts where cart_id = ? and product_id = ?
Cart.select(:quantity).where(:cart_id => cart_id, :product_id => product_id)
如果您只对值感兴趣,而不对对象的开销感兴趣(因此您对任何 Cart 方法、关联等都不感兴趣),那么 pluck 是可用的并且性能更好一些:
Cart.pluck(:quantity).where(:cart_id => cart_id, :product_id => product_id)