I have three models `User`, `Post` and `Favorite`.

```
class User < ApplicationRecord
  has_many :posts
end

class Post < ApplicationRecord
  belongs_to :user
end

class Favorite < ApplicationRecord
  belongs_to :post
  belongs_to :user
end

> user = User.create(name: 'user1')
> user.posts.create(title: 'Title 1')
> user.posts.create(title: 'Title 2')
> Favorite.create(user_id: 1, post_id: 2)

```

I want to retrieve all posts belongs to `user1` with fav status by the user.

```
> User.first.posts_with_fav_status
=>  [#<Post id: 1, title: "Title 1", user_id: 1, faved: false> ],
    [#<Post id: 2, title: "Title 2", user_id: 1, faved: true> ]

```

How can I write the query method like this?

Edit
----

I could get fav status with the following query. But this query calls subquery every time. It will be too slow when DB become bigger. How can I rewrite this query?

```
  def posts_with_fav_status
    posts.select(<<-SQL)
    *, 
    EXISTS(SELECT * FROM favorites 
           WHERE favorites.user_id = posts.user_id
           AND favorites.post_id = posts.id) as faved
    SQL
  end

```