Nested objects ordering of Polymorphic Association
See original GitHub issueDefining Relationship
- User hasMany UserContent
- UserContent hasMany Item
- UserContent hasMany Post
- 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:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@gztchan We’re working on supporting seperate for all includes.
Solved!! Just posing my solution for guys who will face the same problem.
@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.