1

我有 3 个模型,例如帐户、评论和状态。每个Account都会有很多Comment和Status,但是Comment和Status没有任何关系。

我想查询账户的评论和状态,并按时间对这些评论和状态进行排序。我怎样才能做到这一点?

谢谢大家。

4

1 回答 1

1

您可以尝试使用 :through 语句:

class Comment
  belongs_to :user
  has_many :statuses, :through => :user
end


class Status
  belongs_to :user
  has_many :comments, :through => :user
end

和查询:

@user = User.first.includes(:comments, :statuses)

或者

@comment = Comment.first.includes(:user, :statuses)

或者

@statuse = Status.first.includes(:user, :comments)
于 2010-07-23T09:31:02.640 回答