im trying to build a cart in ruby on rails it requires me to show the output like this : You have 3 items in your cart (3 being the number of items in my cart) and im trying to find the number of rows in the table line_items where the cart_id is 5.
@line_items.find(:all, :condition => { :cart_id => "5"}).count
if anyone know how i should write this, please let me know.. Thanks in advance
You can do it the slow way:
or the fast way:
The fast way simply does a
COUNT(*)inside the database, the slow way pulls the whole result set out of the database, turns it into objects, and then counts them.There’s also the modern Rails3+ way:
That does a
select count(*) from t where card_id = 5inside the database. Don’t use this one though:That will do a
select count(card_id = 5) from tand that’s nothing at all like what you want.