I have a User model, that I want to connect to itself through a self join using has_and_belongs_to_many. I have it working almost as expected, except I want it to associate the two users both ways.
My User class:
class User < ActiveRecord::Base
...
has_and_belongs_to_many :friends,
autosave: true,
class_name: 'User',
join_table: :friendships,
foreign_key: :user_id,
association_foreign_key: :friend_user_id
...
end
My migration:
class CreateFriendships < ActiveRecord::Migration
def self.up
create_table :friendships, id: false do |t|
t.integer :user_id
t.integer :friend_user_id
end
add_index(:friendships, [:user_id, :friend_user_id], :unique => true)
add_index(:friendships, [:friend_user_id, :user_id], :unique => true)
end
def self.down
remove_index(:friendships, [:friend_user_id, :user_id])
remove_index(:friendships, [:user_id, :friend_user_id])
drop_table :friendships
end
end
My problem:
user1 = User.find(1)
user2 = User.find(2)
user1.friends << user2
user1.reload.friends.exists?(user2) # true
user2.reload.friends.exists?(user1) # false <- My problem
How can I make the relationship work both ways? As friendships are always mutual in this context and other questions on SO make it seem as this should be possible, I would like for both the last two statements to return true.