Why getting active record error when trying to work on arrays?
Posted
by keruilin
on Stack Overflow
See other posts from Stack Overflow
or by keruilin
Published on 2010-05-19T21:21:42Z
Indexed on
2010/05/19
23:00 UTC
Read the original article
Hit count: 262
I have the following association in my User model:
has_and_belongs_to_many :friends, :class_name => 'User', :foreign_key => 'friend_id'
I have the following uniqueness constraint in my user_users table:
UNIQUE KEY `no_duplicate_friends` (`user_id`,`friend_id`)
In my code, I am retrieving a user's friends --> friends = user.friends. friends is an array.
I have a scenario where I want add the user with all those friends to the friends array. Ex:
friends << user_with_all_those_homies
However, I get the following error:
ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry '18-18' for key 'no_duplicate_friends': INSERT INTO `users_users` (`friend_id`, `user_id`) VALUES (18, 18)
What gives?
© Stack Overflow or respective owner