1
我与DoctorProfile
和Insurance
有多对多的关系。我想从客户端应用程序的表单创建这些关联。我发回一组doctor_insurances_ids
并尝试在一行中创建关联。是否有可能发回一组doctor_insurances
ID?如果是这样的话,在参数中将其命名为批量分配的正确方法是什么?Rails 4使用嵌套属性创建模型has_many
我用下面的代码得到的错误是
ActiveRecord::UnknownAttributeError: unknown attribute 'doctor_insurances_ids' for DoctorProfile.
class DoctorProfile
has_many :doctor_insurances
accepts_nested_attributes_for :doctor_insurances # not sure if needed
class Insurance < ActiveRecord::Base
has_many :doctor_insurances
class DoctorInsurance < ActiveRecord::Base
# only fields are `doctor_profile_id` and `insurance_id`
belongs_to :doctor_profile
belongs_to :insurance
def create
params = {"first_name"=>"steve",
"last_name"=>"johanson",
"email"=>"[email protected]",
"password_digest"=>"password",
"specialty_id"=>262,
"doctor_insurances_ids"=>["44", "47"]}
DoctorProfile.create(params)
end
是的,这就是我现在的,但希望有一个更干净的方式:) – user2954587