Tôi có một người sử dụng và lớp hồ sơ lồng nhau như sau:thuộc tính lồng nhau update_attributes sử dụng chèn chứ không phải cập nhật
class User < ActiveRecord::Base
has_one :profile
attr_accessible :profile_attributes
accepts_nested_attributes_for :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
attr_accessible :name
end
user = User.find(1)
user.profile.id # => 1
user.update_attributes(profile_attributes: {name: 'some name'})
user.profile.id # => 2
Tôi không hiểu tại sao đường ray được ném đi những hồ sơ cũ và tạo ra một cái mới.
Sử dụng
user.profile.update_attributes({name: 'some name'})
chỉ cập nhật hồ sơ cá nhân hiện hành như mong đợi. Nhưng trong trường hợp đó, tôi không tận dụng accept_nested_attributes_for
Có ai biết tại sao cập nhật xảy ra theo cách này không? Tôi không muốn kết thúc với một cơ sở dữ liệu của các hàng hồ sơ không được kết nối với bất kỳ người dùng nào.
có thể là bạn có thể thử, 'user.update_attributes (profile_attributes: {: id => user.profile.id,: name: 'some name'})' –