samedi 27 juin 2015

Rails 4 create model with nested attributes has_many

I have a many to many relationship with DoctorProfile and Insurance. I'd like to create these associations off of a form from a client side app. I'm sending back an array of doctor_insurances_ids and trying to create the association in one line. Is it possible to send back an array of doctor_insurances ids? If so what's the proper way to name it for mass assignment in the params?

The error I'm getting with the following code is

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"=>"steve@ymail.com",
 "password_digest"=>"password",
 "specialty_id"=>262,
 "doctor_insurances_ids"=>["44", "47"]}

  DoctorProfile.create(params)

end

Aucun commentaire:

Enregistrer un commentaire