(这不是我正在使用的实际代码,尽管这总结了我想要做的事情)
class Connection < ActiveRecord::Base
belongs_to :connection1, :polymorphic => true
belongs_to :connection2, :polymorphic => true
end
class User < ActiveRecord::Base
has_many :followers, :class_name => 'Connection', :as => :connection1
has_many :followings, :class_name => 'Connection', :as => :connection2
end
我的问题是我想知道如何创建一个名为“network”的方法,以便返回的不是数组。像这样,
u = User.first
u.network # this will return a merged version of :followings and :followers
这样我仍然可以这样做:
u.network.find_by_last_name("James")
预计到达时间:
或者嗯,我认为我的问题真的可以归结为是否有可能创建一个方法来合并 2 个 has_many 关联,这样我仍然可以调用它的 find_by 方法。