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.