views:

18

answers:

1

I have 3 models, say Account, Comment and Status. Each Account will have many Comment and Status, but Comment and Status are not in any kind of relationship.

I would like to query the Account's Comment and Status, and sort these comments and status by time. How can I make this?

Thanks all.

+1  A: 

You can try to use :through statement:

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


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

And the query:

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

or

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

or

@statuse = Status.first.includes(:user, :comments)
fantactuka
Thanks for reply. But how can I query status and comment at the same time?
siulamvictor
Can you provide mode detailed information about your schema? Also see if updated answer helps
fantactuka
This help a lot. Let me have some tests tonight. Thanks buddy. :)
siulamvictor
To query status and comments, you mean eager loading? As in `Account.all :include => [:comments, :statuses]`
Dmitriy Likhten