Ruby on Rails development from scratch (47)-ActiveRecord Foundation (powerful Find method)

Source: Internet
Author: User
Tags sort ruby on rails

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.