我在某些模型中有一些嵌套属性:
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"=>"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"}}
=> # 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 :(得分:0)
我今天终于可以看到这个问题了。我偶然发现这是一个快速修复,因为我正在处理另一个问题:当我删除Employees,Users或People时,地址没有被删除。修复是这样的:
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
答案 1 :(得分:0)
出于不同的原因,我有这种无声的奇怪行为。在我的模型中,我有
accepts_nested_attributes_for :items, :allow_destroy => true, :reject_if => proc { |attrs| attrs['count'] == '0' || ( attrs['article_id'] == '' && attrs['quantity_id'] == '') }
我删除了:reject_if部分,它再次起作用。