2011-01-22 64 views
0

我正在建立一個社交網絡,我被困在友誼關係中。這是我的數據:
表友誼:友誼協會問題

create_table "friendships", :force => true do |t| 
t.integer "user_id" 
t.integer "friend_id" 
t.boolean "confirmed" 
t.datetime "created_at" 
t.datetime "updated_at" 

協會友誼模式:

class Friendship < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :friend, :class_name => "User" 

協會用戶模型:

has_many :friendships #table that contains the reference of friends 
    has_many :friends, :through=>:friendships, :conditions => ['confirmed = false'] #my performed request to others friends 
    has_many :inverse_friendship, :class_name=>"Friendship", :foreign_key=>"friend_id" #someone requested friendship to me 
    has_many :inverse_friends, :through=>:inverse_friendship, :source=>:user 

在確認布爾表友誼,意思是如果請求被兩個朋友接受,在提交好友請求時,默認爲false。 我想獲得在模型用戶是:

  1. 有很多:當在友誼模型的確認被設置爲false requests_sent#我就是源(USER_ID)
  2. 有很多:當在友誼模型的確認被設置爲false,我的目標(friend_id)
  3. 有許多request_recieved#:#當朋友的友誼模型的確認設置爲true,我是源(user_id)或目標(friend_id)

    您可以從我發佈的代碼中注意到我嘗試了:condition =>'confirmed = false',但Rails將條件應用於用戶模型,而不是友誼模型,所以這不是實現我的3的正確方法目標,在模型中有一個免費的方法User? TNX

PS我不想改變我的模型,我只是想有對現有的解決方案

回答

2

的辦法是把條件友誼模型作爲named_scope ,那麼你應該可以這樣做:

@user.friends.confirmed