以下是我的两个模型User and Patient的简化视图。
用户has_one患者和患者属于用户。
我在rails控制台中尝试做的是:
p = Patient.new(:user_id => 2, :user_attributes => [{:username => 'patient'},{:password => 'password'}])
我收到错误: NoMethodError:[{:username =>“patient”}的未定义方法`with_indifferent_access',{:password =>“password”}]:数组
我做错了什么?
以下是两个模型:
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# username :string(255)
# encrypted_password :string(255)
# salt :string(255)
# active :boolean
# disabled :boolean
# last_login :time
# first_name :string(255)
# last_name :string(255)
# address1 :string(255)
# address2 :string(255)
# city :string(255)
# state :string(255)
# postcode :string(255)
# phone :string(255)
# cell :string(255)
# email :string(255)
# created_at :datetime
# updated_at :datetime
#
class User < ActiveRecord::Base
has_one :patient
attr_accessible :username, :password, :active, :disabled, :first_name, :last_name,
:address1, :address2, :city, :state, :postcode, :phone, :cell, :email
attr_accessor :password
end
# == Schema Information
#
# Table name: patients
#
# id :integer not null, primary key
# user_id :integer
# created_at :datetime
# updated_at :datetime
#
class Patient < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
attr_accessible :user_id, :user_attributes
end
答案 0 :(得分:0)
尝试这样做(删除数组括号,并将所有内容保留在一组哈希花括号中):
p = Patient.new(:user_id => 2, :user_attributes => {:username => 'patient' ,:password => 'password'})
但是,我不确定你为什么要指定一个user_id
并试图创建一个新用户...也许你只是想要这个?
p = Patient.new(:user_attributes => {:username => 'patient', :password => 'password'})