【发布时间】:2010-10-07 11:19:20
【问题描述】:
我将 Accept_nested_attributes_for 与以下模型一起使用:
用户模型:
class User < ActiveRecord::Base
has_many :competences
has_many :skills, :through => :competences, :foreign_key => :skill_id
accepts_nested_attributes_for :skills
end
技能模型:
class Skill < ActiveRecord::Base
has_many :competences
has_many :users, :through => :competences, :foreign_key => :user_id
end
能力模型:
class Competence < ActiveRecord::Base
belongs_to :user
belongs_to :skill
end
技能表有一个“名称”属性。如果已存在具有相同技能名称的记录,我如何让 Accept_nested_attributes_for 不创建新技能记录?
【问题讨论】:
-
您找到解决方案了吗?我的技能设置与
validates :name, presence: true, uniqueness: true完全相同。在嵌套属性表单中输入具有相同名称的技能时,出现唯一性验证错误。如果名称匹配,我希望能够使用现有技能。
标签: ruby-on-rails nested-forms nested-attributes