Ruby on Rails development from scratch (50)-ActiveRecord Basics (Update records)

Source: Internet
Author: User
Tags ruby on rails

Looking at the search method, this time to see how the active record updates the records in the database.

If you have an active Record object (perhaps corresponding to an order table), you can write it to the database by calling the Save method, and if the object was previously read from the database, the Save method will update the existing records, or a new one will be created.

If an existing record is updated, the active records will use its primary key and to match the objects in memory, and the properties in the active Record object are updated to the corresponding columns, even if the values in one column are not changed, in the following example, Order with ID 123 All content will be updated:

order = Order.find (123)
order.name = "Fred"
order.save

In any case, in the following example, an Active Record object contains only Id,name,paytype, and only those fields are updated when the object is saved, and note that if you want to save the object to the database, when you use the Find_by_sql method, Be sure to include the ID field.

Orders = Order.find_by_sql ("Select ID, Name, pay_type from orders where id=123") (a)-A-
orders[0]
first.name = "W Ilma "
First.save

In addition, the Active record provides a Update_attribute () method that can save a property of the model object to the database.

order = Order.find (123)
Order.update_attribute (: Name, ' Barney ') Order
= order.find (321)
Order.update_ Attributes (: Name => "Barney"
: Email => "barney@bedrock.com")

We can combine read and update, use the update () method or Update_all (), the update () method uses an ID and a set of attributes, update the specified property if the corresponding record is in the database, and then return to the model object.

Order = Order.update (in: Name => "Barney": Email => "barney@bedrock.com")

You can also pass a hash of the update () method for a set of IDs or attributes and values, which updates all matching records and returns a set of Model objects.

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.