(
SELECT u.*
FROM friendships f JOIN users u
ON f.friend_id = u.id
WHERE f.user_id = 123
) UNION (
SELECT u.*
FROM friendships f JOIN users u
ON f.user_id = u.id
WHERE f.friend_id = 123
)
class User < ActiveRecord::Base
has_many :friendships
has_many :inverse_friendships, class_name: "Friendship",
foreign_key: :friend_id
def friends
friend_ids = friendships.pluck(:friend_id) +
inverse_friendships.pluck(:user_id)
User.where(id: friend_ids)
end
end
The (Non-Perfect) Mathematics of Trust
Cypher
MATCH (you {name:"You"})
MATCH (expert)-[:WORKED_WITH]->(db:Database {name:"Neo4j"})
MATCH path = shortestPath( (you)-[:FRIEND*..5]-(expert) )
RETURN db, expert, path
has_many :friendships
has_many :inverse_friendships, class_name: "Friendship",
foreign_key: :friend_id
# has_many :friendships
# has_many :inverse_friendships, class_name: "Friendship",
# foreign_key: :friend_id
has_many :both, :friends, type: :friend, model_class: 'User'
def friends
friend_ids = friendships.pluck(:friend_id) +
inverse_friendships.pluck(:user_id)
User.where(id: friend_ids)
end
# def friends
# friend_ids = friendships.pluck(:friend_id) +
# inverse_friendships.pluck(:user_id)
#
# User.where(id: friend_ids)
# end
@user.friends