如何添加现有记录而不是使用嵌套表单和has_many:通过关系创建新记录?

时间:2013-11-12 14:58:11

标签: ruby-on-rails activerecord nested-forms

我有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

当我创建一个新客户端时,我已经为用户嵌套了表单。 目标是:如果新用户不存在则创建新用户,如果已存在则追加。 用户模型有唯一的电子邮件

哪种方法最好?

0 个答案:

没有答案