Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在RubyonRails中,过滤在联接表上有许多按值进行的关系_Ruby_Ruby On Rails 3 - Fatal编程技术网

在RubyonRails中,过滤在联接表上有许多按值进行的关系

在RubyonRails中,过滤在联接表上有许多按值进行的关系,ruby,ruby-on-rails-3,Ruby,Ruby On Rails 3,对于代表朋友的用户模型,我有一个标准的自引用关系。这一部分工作正常,但我在联接表中有一个额外的列,表示该关系的源 友谊模式 # Relationships belongs_to :user belongs_to :friend, :class_name => "User" has_one :source 用户模型 has_many :friendships has_many :friends, :class_name => "User", :through => :frien

对于代表朋友的用户模型,我有一个标准的自引用关系。这一部分工作正常,但我在联接表中有一个额外的列,表示该关系的源

友谊模式

# Relationships
belongs_to :user
belongs_to :friend, :class_name => "User"
has_one :source
用户模型

has_many :friendships
has_many :friends, :class_name => "User", :through => :friendships
 has_many :friendships
 has_many :friends, :class_name => "User", :through => :friendships do 
    def bySource(sourceId)
       where("friendships.source_id = ?", sourceId)
    end
 end
我知道我可以对我用户的朋友进行where筛选

 user.friends.where([some conditions])

我的问题是,如何获得用户对象“朋友”的列表,该列表通过:Friendly中的源关系进行过滤

我刚刚想好了。以防有人好奇

用户模型

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


你甚至不必那么做。你可以在friendships模型上放置一个作用域,名为
by_source
,并以完全相同的方式调用它。听起来不错,我可以根据每个源定义一个作用域。我能举个例子吗?