我在Ruby on rails上有一个具有自引用关联的用户模型
# Friendships
has_many :friendships
has_many :friends, :class_name => "User", :through => :friendships, :uniq => true do
def bySource(sourceId)
where("friendships.source_id = ?", sourceId)
end
end
每个用户都有一个关联的friendStatusDescriptor,用于讲述有关友谊的信息。目前,我将此关联与用户模型绑定
has_many :friendStatuses, :class_name => "FriendStatusDescriptor", :source => :user, :uniq => true
我希望这会回到朋友阵列中。所以,当我做像
这样的事情 render :json => user.friends, :include => :status
列出的每个朋友都有一个名为status的字段,该字段包含此FriendStatusDescriptor对象。如何正确连接这些东西以使其工作?我猜我可以在朋友协会中找到一个方法,但我不确定在渲染过程中如何调用每个朋友的话。
答案 0 :(得分:0)
由于status
描述了用户之间的关联,而不是用户本身,它应该是Friendship
模型的字段或与之关联的模型,而不是附加到User
模型。