ユーザーモデルには次の関連付けがあります。
has_and_belongs_to_many :friends, :class_name => 'User', :foreign_key => 'friend_id'
user_usersテーブルに次の一意性制約があります。
UNIQUE KEY `no_duplicate_friends` (`user_id`,`friend_id`)
私のコードでは、ユーザーの友達を取得しています-> friends=user.friends。友達は配列です。
それらすべての友達を持つユーザーを友達配列に追加したいというシナリオがあります。元:
friends << user
ただし、次のエラーが発生します。
ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry '18-18' for key 'no_duplicate_friends': INSERT INTO `users_users` (`friend_id`, `user_id`) VALUES (18, 18)
何が得られますか?