为什么我这样做时没有分配用户?
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!