Rails Development Details (vi) ActiveRecord Validationa and callbacks validation and callbacks

Source: Internet
Author: User
Tags implement instance method

1. Object life cycle

Typically, in rails applications, objects are created, modified, and deleted. ActiveRecord provides interception for these objects, and you can control your applications and those objects.

Validation guarantees that the data stored in the database is valid. Reconciling the observer allows you to perform some logical operations before and after the state of the object changes.

2. Verify

2.1. Why do I need to verify

Validation guarantees that only legitimate data can be stored in the database. For example, your application needs to ensure that each user has a legitimate e-mail address and mailing address.

There are many ways to verify the legality of the data before you save it to the database. Includes database constraints, client-side validation, controller level validation, model-level validation.

Database constraints or validation in stored procedures are dependent on the database and are difficult to maintain and test. Constraints at the database level are a good idea if your database will be used by other applications. In addition, database-level validation can safely implement things, such as uniqueness constraints, which are relatively difficult to implement in an application.

Client-side validation is useful, but you cannot trust client authentication separately. If you are using JavaScript implementations, you can bypass them. However, with other technologies, client-side validation can give users quick feedback when they visit your site.

Controller level validation is also possible, but often they are cumbersome and difficult to test and maintain. At any time, it's a good idea to keep the controller streamlined and keep your application in a good job for a long time.

Model-level validation is the best way to ensure that legitimate data is stored in a database. They are database-independent and cannot be bypassed by end users, and are easy to test and maintain. In rails, it's easy to use, provides built-in helper methods, and creates your own authentication methods.

2.2. When does the verification occur?

There are two types of ActiveRecord objects: one row of data corresponding to the database and the other not. This record does not exist in the database after you create a new object, that is, after invoking the new method. Once you call the Save method, it is stored in the database. You can use the New_record instance method to determine whether an object exists in a database.

Class Person < activerecord::base 
     
end 
     
p = person.new (: Name => "Shi") 
P.new_record?  #=> false
     
P.save  #=> true
     
p.new_record?  #=> true

The Create and call the Save method sends an INSERT statement to the database, updating an existing record to send an UPDATE statement to the database. Validation occurs before these statements are sent. If the validation fails, the object is marked as illegal and the INSERT or UPDATE statement is not sent, which avoids the illegal data being stored in the database. You can execute the specified validation rules at created,saved and updated.

The following method triggers validation, and if the object is legitimate, it is stored in the database.

Create

create!

Save

save!

Update

Update_attributes

update_attributes!

A method with an exclamation point throws an exception when the object is illegal. Save and Update_attributes without the exclamation mark return false,create and update return objects when they are illegal.

2.3. Skip Validation

The following method skips validation, regardless of whether it is legally stored in the database, and use caution.

decrement!

Decrement_counter

increment!

Increment_counter

toggle!

Touch

Update_all

Update_attribute

Update_column

Update_counters

The Save method can also skip validation by adding: Validate => false parameter, and be careful to use it.

P.save (: Validate => false)

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.