Failed to determine the number of lines of Ruby-On-Rails

im trying to create a basket in ruby ​​on rails, I need to show the result as follows: you have 3 items in your basket (3 - the number of items in my basket), and I'm trying to find the number of rows in the line_items table, where cart_id is 5.

@line_items.find(:all, :condition => { :cart_id => "5"}).count

if anyone knows how i should write this please let me know .. thanks in advance

+3
source share
2 answers

You can do it in a slow way:

YourModelClass.find(:all, :conditions => { :card_id => 5 }).count

or quick way:

YourModelClass.count(:conditions => { :card_id => 5 })

The fast method simply executes COUNT(*)inside the database, the slow method pushes the entire result from the database, turns it into objects, and then counts them.

Rails3 + :

YourModelClass.where(:card_id => 5).count

select count(*) from t where card_id = 5 . , :

YourModelClass.count(:card_id => 5)

select count(card_id = 5) from t , .

+7

-

LineItem.count(:conditions => {:cart_id => 5})

, , ... , =)

+1

All Articles