2010-10-06 73 views
0

我有一些車型的一些嵌套的屬性,例如:沒有的update_attributes工作多到許多嵌套屬性

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 

當我打電話@employee.update_attributes(params[:employee])從我的控制器,它更新的一切除了地址。但是,如果我raise params.inspect並將其複製到腳本/控制檯中的變量,它會工作。例如:

>> 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"=>"[email protected]"}}, 
?> "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"}} 
=> # this outputs the hash that was created 
>> e.update_attributes(params["employee"]) # they are no longer symbols but string keys now 
=> 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"> 

所以你可以看到從腳本/控制檯,但不能從我的控制器更新地址。

如果這是信息超載,那麼這個問題的簡單的版本是:
爲什麼沒有我的地址得到更新?

+0

只是在黑暗中拍攝 - 如果在控制器中使用'params [「employee']'會發生什麼? – zetetic 2010-10-07 00:02:06

+0

@zetetic,感謝您的評論。我認爲這是一個軌道錯誤。我會嘗試重現的步驟,所以我知道肯定。以下是已發佈的故障單:https://rails.lighthouseapp.com/projects/8994/tickets/4766-nested_attributes-fails-to-updatedestroy-when-association-is-loaded-between-setting-attributes-和節能父母。我也會嘗試你的想法。 – DJTripleThreat 2010-10-07 07:15:01

回答

0

我今天終於看到了這個問題。這是我偶然發現的一個問題,因爲我正在處理另一個問題:當我刪除員工,用戶或人員時,地址未被刪除。修復是這樣的:

class AddressPersonLink < ActiveRecord::Base 
    # some how adding dependent => destroy fixed the problem 
    # i was having with updating as well. 
    belongs_to :address, :dependent => :destroy 
    belongs_to :person 
end 
0

我有這種沉默的奇怪的行爲,因爲一個不同的原因。在我的模型中,我有

accepts_nested_attributes_for :items, :allow_destroy => true, :reject_if => proc { |attrs| attrs['count'] == '0' || (attrs['article_id'] == '' && attrs['quantity_id'] == '') } 

我刪除:reject_if部分,並且它再次工作。