question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested objects ordering of Polymorphic Association

See original GitHub issue

Defining Relationship

  1. User hasMany UserContent
  2. UserContent hasMany Item
  3. UserContent hasMany Post
  4. Item hasMany ItemImage
scopes: {
    'content': function () {
        include: [
            { model: sequelize.models.Post, as: 'posts'},
            { 
                model: sequelize.models.Item, 
                as: 'items',
                include: [
                    { model: sequelize.models.ItemImage, as: 'itemImages' }
                ],
                order: [
                    [ { model: sequelize.models.ItemImage }, 'created_at', 'desc' ]
                ]
            }
        ],
        order: [
            ['created_at', 'desc']
        ]
    }
}

So i start to get UserContents, but item_images in item are not ordered by created_at

user.getUserContent({
    scope: ['content']
})

What i expect is:

[
    // this is item object
    {
        "id": 'xxxx',
        "item_images": [
            // images should be ordered
            {
                "created_at": '{timestamp here}' 
            },
            {
                "created_at": '{timestamp here}'
            }
        ]
    },
    // this is post object
    {
        "id": 'xxxx',
        "content": 'xxxxx'
    }
]

Can you tell me how to order nested objects of Polymorphic Association?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mickhansencommented, May 24, 2016

@gztchan We’re working on supporting seperate for all includes.

0reactions
gztchancommented, May 24, 2016

Solved!! Just posing my solution for guys who will face the same problem.

include: [
  {
    model: sequelize.models.Post,
    as: 'post'
  },
  {
    model: sequelize.models.Item,
    as: 'item',
    include: [
      { 
        model: sequelize.models.ItemImage, 
        as: 'itemImages', 
        // -- set separate true
        separate: true,
        order: [
            ['created_at', 'ASC']
        ]
      }
    ]
  }
]

@mickhansen thank you!

By the way, does sequelize has solution to separate “not has many” include ? When model include many other models, the query string will be large because of left join, which make response takes much more time to return.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't save nested model on polymorphic association
Try answer for 2 issues: The correct way is to pass :phones , and then phone as variable to field_for , like is...
Read more >
Rails : nested routes, polymorphic associations and controllers
Let's first create our Category model, as well as the association model that will allow an object to be categorizable. app/models/category.rb
Read more >
Rails polymorphic associations in a nested form in one html ...
Hello everyone, first of all, i am aware that this is a bit of an advanced issue here and i've read the “this...
Read more >
Active Record Associations - Ruby on Rails Guides
The build_association method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link...
Read more >
Polymorphic Associations - Launch Academy Codecabulary
Polymorphic associations add flexibility to the has_many / belongs_to ... need to nest the likes resource under anything that will be likeable in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found