The Active record provides two methods to get the number of records that match the criteria: count () and Count_by_sql (). For example:
C1 = order.count
C2 = Order.count (["name =?", "Dave Thomas"])
C3 = Lineitem.count_by_sql ("SELECT COUNT (*)" +
"from Line_items, orders" +
"where line_items.order_id = orders.id" +
"and orders.name = ' Dave Thomas '")
puts "Dave has #{c3} line items in #{c2} orders (#{c1} orders in all)"
In a program, a database may be accessed by multiple processes or multiple programs, and it is possible to get the latest model objects, which may have just been edited.
In a way, this is mainly used in transactions, anyway, when you need to manually refresh the model object, the Active record can help you, just call the reload () method, the value of the Model object property will be updated by the value in the database.
Stock = Market.find_by_ticker (' RUBY ')
loop do
puts ' price = #{stock.price} ' sleep
stock.reload End