Ruby on Rails development from scratch (45)-ActiveRecord Basics (Read records)

Source: Internet
Author: User
Tags ruby on rails

Reading a record includes specifying those specific data that you are interested in, you specify criteria for the active record, and the active record returns you some objects that contain data that matches the criteria.

The easiest way to retrieve data in a table is to specify the primary key, any model supports the Find () method, which supports one or more primary key values, and if only one primary key is specified, the corresponding object is returned, and if more than one primary key is specified to the Find method, the method sets the corresponding object. Note that when there is no qualifying data, a Recordnotfound exception is thrown, so if the Find method does not throw the exception, the number of objects in the returned array equals the number of IDs specified for the Find method.

An_order = Order.find (+) # Find
the ' order with ID = # Get a list of OrderID from a form, then
# sum the tot Al value
order_list = params[:order_ids]
orders = Order.find (order_list)
count = Orders.size

Typically, you use a value other than the ID in a query, and the Active record provides a set of settings to execute these queries, and we introduce find usage, from basic queries to higher-order dynamic queries.

By now we've just learned the basics of the Find method by specifying IDs to get one or a group of objects. In addition, we can use some tags such as: First,:all as the parameters of the Find method.

: First returns a record that matches the criteria: all returns all eligible records, and next we look at how the active record handles SQL.

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.