Eager load this rails association
Posted
by dombesz
on Stack Overflow
See other posts from Stack Overflow
or by dombesz
Published on 2010-05-10T11:55:49Z
Indexed on
2010/05/10
12:04 UTC
Read the original article
Hit count: 241
Hi, I have rails app which has a list of users. I have different relations between users, for example worked with, friend, preferred. When listing the users i have to decide if the current user can add a specific user to his friends.
-if current_user.can_request_friendship_with(user)
=add_to_friends(user)
-else
=remove_from_friends(user)
-if current_user.can_request_worked_with(user)
=add_to_worked_with(user)
-else
=remove_from_worked_with(user)
The can_request_friendship_with(user) looks like:
def can_request_friendship_with(user)
!self.eql?(user) && !self.friendships.find_by_friend_id(user)
end
My problem is that this means in my case 4 query per user. Listing 10 users means 40 query. Could i somehow eager load this?
© Stack Overflow or respective owner