我正在尝试使用seed.rb文件在我的项目中插入一些默认用户。我在控制台中执行了以下行:
rake db:seed
并且没有抛出任何错误,但也没有创建记录。当我将代码粘贴到 rails 控制台时,再次没有显示错误。我猜我在seed.rb文件中做错了什么。
这就是我的模型的关联方式:
安全用户.rb
has_one :security_users_detail, dependent: :destroy
has_many :security_users_manage_securities
has_many :security_users_roles, through: :security_users_manage_securities
security_users_detail.rb
belongs_to :security_user
安全用户角色.rb
has_many :security_users_manage_securities
has_many :security_users, through: :security_users_manage_securities
这是我在seed.rb文件中的代码:
users = {
Admin: {
Information: {
email: 'test@gmail.com',
password: 'test',
password_confirmation: 'test'
},
Details: {
address: 'Not defined.',
city: 'Not defined.',
country: 'Not defined.',
egn: '0000000000',
faculty_number: '',
first_name: 'Admin',
gender: 'male',
gsm: '0000000000',
last_name: 'Not defined.',
skype: 'Not defined.'
},
Roles: %w(Administrator)
}
}
users.each do |user, data|
security_user = SecurityUser.new(data[:Information])
data[:Roles].each { |role|
security_user.security_users_manage_securities.build(security_users_role: SecurityUsersRole.find_by_role(role))
}
SecurityUser.where(email: security_user.email).first_or_create!(security_user.attributes)
security_users_detail = SecurityUsersDetail.new(data[:Details])
security_users_detail.security_user_id = security_user.id
SecurityUsersDetail.where(security_user_id: security_users_detail.security_user_id).first_or_create
end