1

アカウント、コメント、ステータスの 3 つのモデルがあります。各アカウントには多くのコメントとステータスがありますが、コメントとステータスは何の関係もありません。

アカウントのコメントとステータスを照会し、これらのコメントとステータスを時間で並べ替えたいと思います。どうすればこれを作ることができますか?

皆さんありがとう。

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 に答える