Rake DB: Seed instance

Source: Internet
Author: User
ArticleDirectory
    • 1. Create a model named blog

During website development, databases often change, and repeated input of some test data is very troublesome. In this case, you can use rake DB: seed to initialize some data, which will call dB/seeds. RB file, instance description.

1. Create a model named blog

Run rails G model Blog content: string created: datetime title: Text

A new app/models/blog. RB will be created with the following content:

Class blog <activerecord: Base
Attr_accessible: content,: created,: Title
End

 

2. Open the DB/seeds. RB file and add the following statement:

# Encoding: UTF-8

T = time. Now
# Blog. Create (: Title => "first Chinese version",: created => T,: content => "first Chinese version ")

# The following error will be reported for rake DB: Seed during initialization:
# Rake aborted!
# Can't mass-assign protected attributes: title, created, content
# Blog. Create (: Title => "first Chinese version",: created => T,: content => "first Chinese version ")

 

Blog. Create ({: Title => "first Chinese version",: created => T,: content => "first Chinese version "})
Blog. Create ({: Title => "Chinese Version 2",: created => T,: content => "People's Republic of China "})
# If time is required, write it directly without adding # {}, which indicates a string.
# Blog. Create ({: Title => "third edition of Chinese",: created =>#{ time. Now},: content => "People's Republic of China "})
Blog. Create ({: Title => "third edition of Chinese",: created => time. Now,: content => "People's Republic of China "})

 

3. Run rake DB: seed.

Run rails S and enter http: // 192.168.1.107: 3000/blogs in the browser.

:

The data in seeds. Rb is saved to the database file dB/development. sqlite3.

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.