Create seed file from data already in the database

后端 未结 4 503
再見小時候
再見小時候 2020-12-07 20:06

I\'m using Rails 3.0.3 and have data for my \"categories\" table already in the database, but want to create a seed file from it. Is there any rake task that will generate t

4条回答
  •  半阙折子戏
    2020-12-07 20:41

    Not sure about any existing rake tasks, but you can try running something like this in the rails console & paste the results into your seeds.rb file

    (warning: dirty & untested)

    c = Category.all
    
    c.each do |cat|
      puts "Category.create(:name => '#{cat.name}')"
    end
    

    Adjust for any additional fields you may have.

    Hope this helps.

提交回复
热议问题