在轨道上结合红宝石中的关联

时间:2013-08-29 17:25:55

标签: ruby-on-rails ruby

我在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对象。如何正确连接这些东西以使其工作?我猜我可以在朋友协会中找到一个方法,但我不确定在渲染过程中如何调用每个朋友的话。

1 个答案:

答案 0 :(得分:0)

由于status描述了用户之间的关联,而不是用户本身,它应该是Friendship模型的字段或与之关联的模型,而不是附加到User模型。