开发者

active record where foreign key can be anything of two columns

开发者 https://www.devze.com 2023-03-12 10:01 出处:网络
I have a User Model. I have a Friend Model with columns invitee_id and Inviter_id and status. Status is used as flag whether friend request has been accepted or not. Inviter_id is the id of the user

I have a User Model.

I have a Friend Model with columns invitee_id and Inviter_id and status. Status is used as flag whether friend request has been accepted or not. Inviter_id is the id of the user who sending the friend request and invitee_id is the user who is receiving the friend request.Please chec开发者_开发知识库k inline comment.

class User < ActiveRecord::Base
    has_many :friends // now i want to search all the friends with accepted friend request. (sent or received both.)
    has_many :pending_friend_requests,:class_name => "Friend", :foreign_key=>"invitee_id", :conditions => {:status => 0}
end

class Friend < ActiveRecord::Base

end

Question is how to fetch all the friend with accepted friend requests.. sent or received as there are two foreign columns. invitee_id or Inviter_id


If I got your question right, this screencast is what you need.

Updated

Altough you say you don't, I think, you do need a self-referential many-to-many relation. Instead of making an associations for pending requests, you could make a named scope. And after that, you could get all the friends you invited by User.find(params[:id]).friends.accepted.

What I can't understand is whether you want user.friends to retrieve both the ones who invited me and the ones invited by me OR only one of these.

Because of your namings (inviter and invitee) I suppose it is the second case. An it is covered in the screencast. It is done by creating additional inverted association (Ryan talks about that in the end).

But if it's the first one, the easiest solution would be to make two rows for each inviter-invitee pair. You could use this gem to simplify things, but it acts the same way I told.

If it doesn't help, try to specify your question.


The has_many sets up the relations. Use scope for conditions.

class User < ActiveRecord::Base
    has_many :invitees, :through => :friends
    has_many :friends, :foreign_key=>"inviter_id"

    def accepted_invitees
       invitees.where(:friends => {:accepted => true })
    end
end

class Friend < ActiveRecord::Base
    belongs_to :invitee, :class_name => "User"
    belongs_to :inviter, :class_name => "User"

    # needs accepted column
end

However, this is way to confusing because of the way that the models and columns are setup. If I were doing this I would do something like:

class User < ActiveRecord::Base
    has_many :friends, :through => :friendships
    has_many :friendships

    def accepted_friends
        friends.where(:friendships => {:accepted => true })
    end

end

class Friendships < ActiveRecord::Base
    belongs_to :user # change inviter_id to user_id
    belongs_to :friend, :class_name => "User" # change invitee_id to friend_id

    # needs accepted column
end
0

精彩评论

暂无评论...
验证码 换一张
取 消