【发布时间】:2016-09-06 09:02:21
【问题描述】:
class User < ApplicationRecord
has_and_belongs_to_many :profiles
def add_profile(profile)
self.profiles << profile unless self.profiles.include?(profile)
end
end
class Profile < ApplicationRecord
has_and_belongs_to_many :users
validates_uniqueness_of :linkedin_id, allow_nil: true
end
出于某种生产原因,我得到了
ActiveRecord::RecordInvalid: Validation failed: Linkedin has already been taken
在
self.profiles << profile unless self.profiles.include?(profile) 行。
在此之后,我在 User.profiles 记录中有重复项。
有什么问题?
【问题讨论】:
-
看起来新的配置文件已创建。您能否添加引发错误的上下文?
-
您为什么使用
has_and_belongs_to_many关系?为什么个人资料会属于许多用户? -
@max 因为特定的配置文件可以属于多个用户。
标签: ruby-on-rails activerecord ruby-on-rails-5