ruby on rails - update_attributes not working for many-to-many nested attributes -
i have nested attributes in models such:
class employee < activerecord::base has_one :user, :as => :user_role, :dependent => :destroy accepts_nested_attributes_for :user, :allow_destroy => true end class user < activerecord::base has_one :person, :as => :person_role, :dependent => :destroy belongs_to :user_role, :polymorphic => true accepts_nested_attributes_for :person, :allow_destroy => true end class person < activerecord::base has_many :address_person_links, :dependent => :destroy has_many :addresses, :through => :address_person_links, :uniq => true, :dependent => :destroy belongs_to :person_role, :polymorphic => true accepts_nested_attributes_for :addresses, :allow_destroy => true end class addresspersonlink < activerecord::base belongs_to :address belongs_to :person end class address < activerecord::base has_many :address_person_links, :dependent => :destroy has_many :people, :through => :address_person_links, :uniq => true end
when call @employee.update_attributes(params[:employee])
controller updates except address. if raise params.inspect
, copy variable in script/console, work. example:
>> e = employee.find(8) => #<employee id: 8, active: true, admin: false, created_at: "2010-10-06 20:05:01", updated_at: "2010-10-06 20:11:20"> >>address = a.user.person.addresses[0] => #<address id: 10, address1: "225 3rd ave", address2: "", address3: "", city: "sacramento", state_id: 5, zip_code: "95814", country_id: 1, contact_type_id: 2, created_at: "2010-10-06 20:05:01", updated_at: "2010-10-06 22:40:06"> >> params = {"commit"=>"update", ?> "_method"=>"put", ?> "authenticity_token"=>"sygfndbt4sb00wsjjxnpf4fnhrt4hbhcy7w+ienpc/k=", ?> "id"=>"8", ?> "employee"=>{"user_attributes"=>{"person_attributes"=>{"addresses_attributes"=>{"0"=>{"address1"=>"225 3rd ave suite 777", ?> "city"=>"sacramento", ?> "contact_type_id"=>"2", ?> "address2"=>"", ?> "address3"=>"", ?> "zip_code"=>"95814", ?> "country_id"=>"1", ?> "id"=>"10", ?> "state_id"=>"5"}}, ?> "prefix"=>"", ?> "email_addresses_attributes"=>{"0"=>{"contact_type_id"=>"2", ?> "id"=>"16", ?> "email"=>"first@example.com"}}, ?> "id"=>"16", ?> "last_name"=>"last", ?> "suffix"=>"", ?> "phone_numbers_attributes"=>{"0"=>{"number"=>"9165555555", ?> "contact_type_id"=>"1", ?> "extension"=>"", ?> "id"=>"16"}}, ?> "first_name"=>"first"}, ?> "password_confirmation"=>"321321", ?> "id"=>"16", ?> "password"=>"321321", ?> "login"=>"third"}, ?> "admin"=>"0", ?> "active"=>"1"}} => # outputs hash created >> e.update_attributes(params["employee"]) # no longer symbols string keys => true >> address => #<address id: 10, address1: "225 3rd ave suite 777", address2: "", address3: "", city: "sacramento", state_id: 5, zip_code: "95814", country_id: 1, contact_type_id: 2, created_at: "2010-10-06 20:05:01", updated_at: "2010-10-06 22:40:16">
so can see address was updated script/console not controller.
if information overload, simple version of question is:
why doesn't address updated?
i able @ problem today. quick fix stumbled upon because dealing issue: addresses weren't being deleted when deleted employees, users or people. fix this:
class addresspersonlink < activerecord::base # how adding dependent => destroy fixed problem # having updating well. belongs_to :address, :dependent => :destroy belongs_to :person end
Comments
Post a Comment