开发者

Foreign key search fails in Rails 3

开发者 https://www.devze.com 2023-01-27 00:52 出处:网络
I am coding a small community in Rails 3 and I got two tables, Profiles and Contacts. When a user adds a friendship with another user it is saved in a table called Contacts which holds two columns; pr

I am coding a small community in Rails 3 and I got two tables, Profiles and Contacts. When a user adds a friendship with another user it is saved in a table called Contacts which holds two columns; profile_id and friend_开发者_运维百科id.

Profile_id: This is where the users ID is saved Friend_id: This is where the other users ID is saved

If a another user adds the user as a friend I want it to show up on the users home screen so that he can add the other user as well, but I only want it to show up if the user does not already have the other user as a friend.

I have tried the code below but it doesn't seem to work as I want it to.

@connections = Contact.where(["friend_id = ?", params[:profile_id]])

@notfriends = @connections.find_all {|profile| Contact.where(["profile_id = ? AND friend_id = ?", profile.friend_id, params[:profile_id]])}

Any ideas what is wrong? Is this the correct syntax?

UPDATE

So what I am looking to achieve is:

  1. Get all contacts where the user is set as friend (friend_id).
  2. Then I will would like to only get the contacts from the above query which the user does not already have as a friend (profile_id).


In line 2 of your code params[:profile_id] and profile.friend_id are necessary the same... since in your first query you search for entry where friend_id == params[:profile_id].

0

精彩评论

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