Now that we know how to specify conditions, let's look at some of the other settings supported by the Find method.
First of all, understand find (: primary,...) method is very important, and the method is set under the same conditions, and find (: All,...) method generates the same SQL statement, except that a single record is returned. At the time of invocation, one of the arguments is: the one, and the other: all.
The Find method executes an SQL statement such as a select from, where the all tag specifies that all records in the table are returned: First returns a record. But now: First, there's no guarantee that you'll get what you've got in the table, what's the reason? We continue to look down.
: The conditions parameter specifies the where part of the SQL statement, which can contain the SQL statement or the name of the parameter that can be substituted, the value, which we have already learned.
Daves_orders = Order.find (: All,: Conditions => "name = ' Dave '")
name = Params[:name]
other_orders = Order.find (: All,: Conditions => ["name =?", name])
Yet_more = Order.find (: All,
: Conditions => ["name =: Name and Pay_type =:p Ay_type",
params])
The Find method above does not guarantee that records are returned in a particular order, unless you specify the ordering part of the query. : The order parameter is used to specify the sort criteria for the SQL, and the following example shows a query whose name is Dave and follows the Pay_type,shipped_at field in descending order.
Orders = Order.find (: All,
: Conditions => "name = ' Dave '",
: Order => "Pay_type, Shipped_at DESC")
We can also set: limit parameters to limit the number of records returned, if you use: Limit parameters, you may also want to specify the sort criteria, the following example returns 10 records, and sorted by the specified criteria:
Orders = Order.find (: All,
: Conditions => "name = ' Dave '",
: Order => "Pay_type, Shipped_at DESC",
: Limit => 10)
Parameter: Offset often appears with the: Limit parameter, which specifies that the specified offset is returned from the first record, and the following code demonstrates the use of the offset parameter:
def order.find_on_page (Page_num, page_size) Find
(: All,
: order => "id",
: Limit =>-Page_size,
: o Ffset => page_num*page_size) End