How (and whether) to populate rails application with initial data

后端 未结 13 1708
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 05:43

I\'ve got a rails application where users have to log in. Therefore in order for the application to be usable, there must be one initial user in the system for the first pe

13条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 06:31

    Use db/seed.rb found in every Rails application.

    While some answers given above from 2008 can work well, they are pretty outdated and they are not really Rails convention anymore.

    Populating initial data into database should be done with db/seed.rb file.

    It's just works like a Ruby file.

    In order to create and save an object, you can do something like :

    User.create(:username => "moot", :description => "king of /b/")

    Once you have this file ready, you can do following

    rake db:migrate

    rake db:seed

    Or in one step

    rake db:setup

    Your database should be populated with whichever objects you wanted to create in seed.rb

提交回复
热议问题