0
比方說,我有簡單的2種相關機型:如何更新一個ActiveRecord和它的「has_and_belongs_to_many」對象相關
class User < ActiveRecord::Base
has_and_belongs_to_many :emails
accepts_nested_attributes_for :emails
end
class Email < ActiveRecord::Base
has_and_belongs_to_many :users
end
如何可以將用戶的實例得到更新,如果我改變其郵件數組:
@user = User.new(username: "ben", password: "ben123")
@user.emails.build(:address: "[email protected]", app: "outlook")
@user.save!
@user.update(password: "321neb", emails: [{id: 1, app: "gmail"}])
更新行不適用於我,那麼更新的正確方法是什麼?