0

私は、友人を表すユーザーモデルの標準的な自己参照関係を持っています。その部分は正常に機能しますが、結合テーブルにその関係のソースを表す追加の列があります。

友情モデル

# Relationships
belongs_to :user
belongs_to :friend, :class_name => "User"
has_one :source

ユーザーモデル

has_many :friendships
has_many :friends, :class_name => "User", :through => :friendships

ユーザーの友達にwhereフィルターを実行できることを理解しています

 user.friends.where([some conditions])

私の質問は、Friendshipの:source関係によってフィルタリングされたユーザーオブジェクト「friends」のリストを取得するにはどうすればよいですか?

4

1 に答える 1

0

ちょうどこれを理解しました。誰かが興味を持った場合に備えて。

ユーザーモデル

 has_many :friendships
 has_many :friends, :class_name => "User", :through => :friendships do 
    def bySource(sourceId)
       where("friendships.source_id = ?", sourceId)
    end
 end

使用法

 users.friends.bySource(1)
于 2013-03-26T17:54:53.877 に答える