Ruby on Rails Tutorial Sixth user model

Source: Internet
Author: User
Tags ruby on rails

1. User model
(1) Database migration
Rails uses a relational database to store data by default, with tables in the database consisting of data rows, each row having corresponding columns, and corresponding data properties. Once the column names are named, ActiveRecord automatically identifies them as attributes of the user object.

# to generate the user controller and the new action, the controller name is retelling users # generate user model, model name is singular user exec # Move Up exec # Migrate down

migration is a way to modify the structure of a database to modify the data model as needed. After executing the Generte command, the migration is automatically created for the user model , which is created by creating a users table and two columns for name and email.

(2) Model file
A: Create A User Object

>> user=user.new (name:"amysun", Email:"12***@**.com" # Create # Save

The above two steps are equivalent to the following step, which is to create and save the composition in one step:

>> foo=user.create (name:"amysun", Email:"12***@**.com  ")#reverse operation of Create

B: Find User objects

# Find by User ID >> user.find_by (email:"12***@**.com"# through property lookup, If the number of users is too high, the efficiency of using find_by # returns the first user in the database # Returns a Activerecord:relation instance, which is actually an array , including all users in the database

C: Update User objects

>> user.email="[email protected]">> user.save

Or

>> user.update_attributes (name:"lilysun", Email:"[email protected]  # Update multiple attribute values >> user.update_attribute (name:"lilysun"  # Update a single property value

2. User Data verification
Several common data validations: presence, length, format, and uniqueness

# add an index to the user's email attribute exec Rake db:migrate

The code for the user class that has been added is as follows:

class User < activerecord::base    before_save {email.downcase!}     * =/\a[\w+\-.]     [Email protected] [A-z\d\-]+ (\.[ a-z\d\-]+) *\. [a-z]+\z/i    validate:email, presence:true,            format:    {With:valid_email_regex},            uniqueness: { Case_sensitive:false}    Has_secure_password    6 }end

Ruby on Rails Tutorial Sixth user model

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.