我有 3 个模型。它们是:
class Client < ActiveRecord::Base
has_many :users, through: :client_users
has_many :client_users
accepts_nested_attributes_for :users, allow_destroy: true
accepts_nested_attributes_for :client_users, allow_destroy: true
end
class ClientUser < ActiveRecord::Base
belongs_to :client
belongs_to :user, autosave: false
accepts_nested_attributes_for :client
accepts_nested_attributes_for :user
end
class User < ActiveRecord::Base
has_many :clients, through: :client_users
has_many :client_users
accepts_nested_attributes_for :client_users
end
当我创建一个新客户端时,我为用户嵌套了表单。 目标是:如果新用户不存在则创建一个新用户,如果它已经存在则追加。用户模型具有唯一的电子邮件。
最好的方法是什么?