Ruby attr_accessible, validates, attr_accessor, cattr_access

Source: Internet
Author: User

When attr_accessible is created, the Model contains attr_accessible. The full name is attibutes accessible, which literally means that attributes can be accessed, for example, rails g model User name: string email: string, the following files are created and automatically added to attr_accessible class User <ActiveRecord: Base attr_accessible: name,: emailendend. If there is no attr_accessible, access is disabled by default. This is a security mechanism. validates verification validates_presence_of, expires, expires, allow_nil, allow_blank, message, onRails3 after validates: email,: presence => true,: length =>{: minimum => 3 ,: maximum => 254,: message => "incorrect length"},: uniqueness => true,: email => true validate: my_validation errors [: base] <"can not be xxx" attr_reader and attr_accessor are mainly used to set or read attribute values in the class. attr_reader is read-only and cannot be set, while attr_accessor can be read. You can also set cattr_accessorcattr_accessor, which is equivalent to static variables of java classes and shared with all class instances.

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.