我有简单的rails 2 composer app。我可以在本地rake迁移和种子应用程序,并设置种子文件中的管理员用户。但是db不会在Heroku上播种。我得到以下错误(当我运行时跟踪 - heroku运行rake db:setup --trace):
** Execute db:abort_if_pending_migrations
ROLES
rake aborted!
can't convert nil into String
这是我的代码:
seed.rb
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
# Environment variables (ENV['...']) are set in the file config/application.yml.
# See http://railsapps.github.io/rails-environment-variables.html
puts 'ROLES'
YAML.load(ENV['ROLES']).each do |role|
Role.find_or_create_by_name({ :name => role }, :without_protection => true)
puts 'role: ' << role
end
puts 'DEFAULT USERS'
user = User.find_or_create_by_email :name => ENV['ADMIN_NAME'].dup, :email => ENV['ADMIN_EMAIL'].dup, :password => ENV['ADMIN_PASSWORD'].dup, :password_confirmation => ENV['ADMIN_PASSWORD'].dup
puts 'user: ' << user.name
user.add_role :admin
user.save!
application.yml
GMAIL_USERNAME: Your_Username
GMAIL_PASSWORD: Your_Password
ADMIN_NAME: First User
ADMIN_EMAIL: user@example.com
ADMIN_PASSWORD: changeme
ROLES: [admin, user]
我是铁杆的新手。该应用程序最初确实播种,但我已经进行了一些迁移并回滚了一次。
任何帮助都非常感激。
答案 0 :(得分:0)
如果您使用“db:reset --trace”跟踪角色并添加“put role”,如下所示:
YAML.load(ENV ['ROLES'])。每个do | role | 放角色
你可以看到第一个角色是admin,但是在rake db:reset之后把消息“uninitialized constant Role”
所以问题不在于ENV ['ROLES']!