我正在寻找遵循指示的外键约束的种子数据。
我收到消息“未定义的方法'创建'为nil:NilClass”
我的命令:
rails g model user login:string password:string
rails g model account gender:string age:integer first_name:string last_name:string user:references
User.delete_all
Account.delete_all
@Jared = User.create [{login: "Jared", password: "password2"}]
@Jared.first.account.create([{gender: "male", age: 99, first_name: "Irish", last_name: "Coder"}])
acct = Account.create [{gender: "male", age: 99, first_name: "Irish", last_name: "Coder"}]
@Jared.account = acct
class User < ActiveRecord::Base
has_one :account
end
class Account < ActiveRecord::Base
belongs_to :user
end
答案 0 :(得分:1)
@Jared = User.create [{login: "Jared", password: "password2"}]
@Jared.first.account.create([{gender: "male", age: 99, first_name: "Irish", last_name: "Coder"}])
此部分为零
@Jared.first
尝试:
defaults = User.create(login: "Jared", password: "password2")
User.create{login: defaults.login, password: defaults.password, gender: "male", age: 99, first_name: "Irish", last_name: "Coder"}