これを行ったときにユーザーが割り当てられないのはなぜですか?
u1 = User.create( :username => "user1", :password => "testing", :email => "user1@email.com", :time_zone => "Tokyo")
u2 = User.create( :username => "user2", :password => "testing", :email => "user2@email.com", :time_zone => "Eastern Time (US & Canada)")
u3 = User.create( :username => "user3", :password => "testing", :email => "user3@email.com", :time_zone => "Singapore") # User that adds Private stuff only.
o1 = Owner.create!( :name => "Owner A", :about => "", :user_id => User.find_by_username("user1"), :private => false, without_protection: true )
o2 = Owner.create!( :name => "Owner B", :about => "", :user_id => User.find_by_username("user2"), :private => false, without_protection: true )
o3 = Owner.create!( :name => "Private Owner", :about => "", :user_id => User.find_by_username("user3"), :private => true, without_protection: true )
エラーが発生します:
$ rake db:seed
rake aborted!
Validation failed: User can't be blank
Tasks: TOP => db:seed
(See full trace by running task with --trace)
ユーザーIDを割り当てることができる他の方法は何ですか?
アップデート
オーナー.rb
class Owner < ActiveRecord::Base
attr_accessible :name, :about, :private
belongs_to :user
has_many :locations
validates_presence_of :name
validates_presence_of :user_id
validates_inclusion_of :private, :in => [true, false]
validates_length_of :name, :in => 3...50, :allow_blank => true
end
これまでのところ、私は試しました:
:user_id => User.find_by_username("user1")
:user_id => u1
:user_id => u1.id
:user => User.find_by_username("user1")
:user => u1
更新 2
user_id = 1
これは機能しますが、すべての所有者に対してのみ保存されます。
o1 = Owner.new( :name => "Owner A", :about => "", :private => false )
o1.user_id = u1
o1.save!