Hello I have Room and Messages tables where where room has many messages. I want to get user rooms with limit 20 rooms and each room has only 20 messages only.

type Room struct {
    gorm.Model
    ID            uint        `json:"id" gorm:"primary_key"`
    Hash          string      `json:"hash" binding:"required" gorm:"not null:true"`
    Users         []User      `json:"users" gorm:"many2many:room_users"`
    Messages      []Message   `json:"messages"`
    Blocked       bool        `json:"blocked" gorm:"default:false"`
    BlockerId     uint        `json:"blocker_id" gorm:"nullable:true"`
    CreatedAt     time.Time   `json:"created_at"`
    UpdatedAt     time.Time   `json:"updated_at"`
    RoomBotData   shared.RoomBotData `json:"bot_data"`
    RoomBotDataID uint        `json:"room_bot_data_id"`
}
type Message struct {
    ID        uint      `json:"id" gorm:"primary_key"`
    Text      string    `json:"text" binding:"required" gorm:"not null:true"`
    UserID    uint      `json:"user_id" gorm:"not null:true"`
    RoomID    uint      `json:"room_id" gorm:"not null:true"`
    IsRead    bool      `json:"is_read" binding:"required" gorm:"default:false"`
    Type      string    `json:"type" gorm:"default:text"`
    CreatedAt time.Time `json:"created_at"`
}

I have tried preload function and set limit to 20 for message


db.Preload("Messages", func(db *gorm.DB) *gorm.DB {
  return db.Limit(20)
}).Find(&rooms)

but this only get 20 messages in total for all rooms.

I also tried to get 20 messages for each room ```golang

err = db.DB.Model(&Room{}).
    Order("updated_at DESC").
    Limit(20).
    Preload(RoomSchema.Users).
    Where("id in ?", roomIds).
    Find(&rooms).Error

roomMessagesChannel := make(chan map[int][]Message)

for i, room := range rooms {
    go r.getRoomMessages(roomMessagesChannel, room.ID, i)
}
for i := 0; i < len(rooms); i++ {
    messagesMap := <-roomMessagesChannel
    firstItemOfMap := reflect.ValueOf(messagesMap).MapKeys()[0].Int()
    rooms[int(firstItemOfMap)].Messages = messagesMap[int(firstItemOfMap)]

}

close(roomMessagesChannel)
```

I used channels to speed up the process but it cause memory leak and consume a lot of memory. I want to do that in a better way

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: a631807682

It's hard to support this situation by gorm, you can limit the number of goroutines to reduce memory.

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: jinzhu

refer https://github.com/go-gorm/gorm/issues/5229