Home>
  • Room table
ID name
1 room1
  • User table
ID room_id name
1 1 User 1
2 1 User 2
  • Post table
ID room_id user_id name
1 1 1 post1
2 1 2 post2
  • Comment table
ID room_id user_id post_id message
1 1 1 1 comment1
2 1 1 1 comment2
3 1 2 2 comment3
  • Asset table
ID room_id user_id post_id comment_id asset_type file_path
1 1 1 1 null image XXX/YYY
2 1 2 null null image XXX/YYY
3 1 1 1 2 image XXX/YYY
Four 1 2 2 3 image XXX/YYY
Five 1 2 2 3 movie XXX/YYY
class Room  {where (asset_type: "image")}, class_name: "Asset"
  has_many: movies,->{where (asset_type: "movie")}, class_name: "Asset"
end
class Post  {where (asset_type: "image")}, class_name: "Asset"
  has_many: movies,->{where (asset_type: "movie")}, class_name: "Asset"
end
class Comment  {where (asset_type: "image")}, class_name: "Asset"
  has_many: movies,->{where (asset_type: "movie")}, class_name: "Asset"
end
class Asset</pre>
<p>We are creating a Rails application with the above association.<br />
The simple writing in Japanese is as follows.</p>
<ul>
<li>Room has multiple Users</li>
<li>User has multiple Posts and multiple images and movies</li>
<li>Post has multiple Comments and multiple images and movies</li>
<li>Comment has multiple images and movies</li>
<li>Asset (image and movie entity) is linked to User, Post and Comment</li>
</ul>
<p>Let's assume that each model has the ID of the model to which it belongs as a foreign key.<br />
Example: User model has room_id, Asset model has user_id, post_id and comment_id ...</p>
<p>Here is the main question,</p>
<pre><code>Income all the above information associated with a room using includes so that the number of SQL issuance is as small as possible


How should I write it?

I'm wondering if it will be implemented as follows, but this is not enough recognition.

room_id = 1 # example
Room.find_by (id: room_id) .users.includes (posts: {comments: [: movies,: images]})

Postscript:
I wrote that with includes,
If the above contents (acquire information of related tables together with less SQL) can be achieved
It doesn't matter how you do it.

  • Answer # 1

    If that is the case, you will get users, not room.

    Room has multiple Users
    User has multiple Posts and multiple images and movies
    Post has multiple Comments and multiple images and movies
    Comment has multiple images and movies

    Room.includes (users: [: images,: movies, posts: [: movies ,: images, comments: [: movies ,: images] ] ] ) .find (room_id)
    Wonder?
    "Asset (image and movie entity) is linked to User, Post and Comment"
    Since the related definitions, Asset, image and movie definitions are not listed here, the image does not come out. (I know it's thin, but it might be strange to write it by estimation.)

    Postscript
    able or not,,,
    Looking at the data example, it seems that it always belongs to room, so declare this relationship and also define the relationship from Asset to user, post, comment
    assets = Asset.includes (: room,: users,: posts,: comments)
    (Maybe Asset.includes (: users ,: posts ,: comments, room: {users: {posts :: comments}) ...)
    After that
    assets.map (&: rooms) .uniq
    It will be an Array.
    I don't think you can get assets in Asset.includes (...). Room ...