Double Nested Includes
up vote
0
down vote
favorite
I have a navigation structure stored in a database. There are two models Navigation and Navigation Items.
class Navigation < ApplicationRecord
has_many :navigation_items
scope :all_items, -> {
includes(navigation_items: [:translations, children: :translations])
.order('navigation_items.position asc')
.where(navigation_items: { parent_id: nil })
}
end
class NavigationItem < ApplicationRecord
has_many :children, class_name: "NavigationItem", foreign_key: "parent_id"
belongs_to :parent, class_name: "NavigationItem", foreign_key: 'parent_id', optional: true
belongs_to :navigation
end
I setup the navigations in the application controller like this
def set_navigation
@navigations = Navigation.all_items
@navigation =
@footer_navigation =
@header_navigation =
if (main = @navigations.detect { |n| n.handle == "main" })
@navigation = main.navigation_items
end
if (footer = @navigations.detect { |n| n.handle == "footer" })
@footer_navigation = footer.navigation_items
end
if (header = @navigations.detect { |n| n.handle == "header" })
@header_navigation = header.navigation_items
end
end
I then loop through each navigation_item in the layout with a nested loop for the children.
All is working well except one thing. The navigation items all have a position tied to them. The parent items display in the correct order however the children are not obeying the order. Is there a way to also scope the children navigation items to order by position?
ruby-on-rails navigation sql-order-by rails-activerecord model-associations
add a comment |
up vote
0
down vote
favorite
I have a navigation structure stored in a database. There are two models Navigation and Navigation Items.
class Navigation < ApplicationRecord
has_many :navigation_items
scope :all_items, -> {
includes(navigation_items: [:translations, children: :translations])
.order('navigation_items.position asc')
.where(navigation_items: { parent_id: nil })
}
end
class NavigationItem < ApplicationRecord
has_many :children, class_name: "NavigationItem", foreign_key: "parent_id"
belongs_to :parent, class_name: "NavigationItem", foreign_key: 'parent_id', optional: true
belongs_to :navigation
end
I setup the navigations in the application controller like this
def set_navigation
@navigations = Navigation.all_items
@navigation =
@footer_navigation =
@header_navigation =
if (main = @navigations.detect { |n| n.handle == "main" })
@navigation = main.navigation_items
end
if (footer = @navigations.detect { |n| n.handle == "footer" })
@footer_navigation = footer.navigation_items
end
if (header = @navigations.detect { |n| n.handle == "header" })
@header_navigation = header.navigation_items
end
end
I then loop through each navigation_item in the layout with a nested loop for the children.
All is working well except one thing. The navigation items all have a position tied to them. The parent items display in the correct order however the children are not obeying the order. Is there a way to also scope the children navigation items to order by position?
ruby-on-rails navigation sql-order-by rails-activerecord model-associations
Would adefault_scope
forNavigationItem
work? How about adding anorder
when callingnavigation_items
? I believe that theorder
for theall_items
scope only applies to the initial query forNavigation
, not to the subsequent queries for theincludes
relations.
– shanecav
Nov 10 at 23:09
I'm wrong about theorder
only applying to the initial query, at least on Rails 5.2.1. Which version of ActiveRecord are you on?
– shanecav
Nov 10 at 23:16
Hey @shanecav, also on rails 5.2.1. It is applying to the first level of navigation items but it is the sub items that are having trouble. I have tried default_scope with no luck.
– trowse
Nov 11 at 22:09
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a navigation structure stored in a database. There are two models Navigation and Navigation Items.
class Navigation < ApplicationRecord
has_many :navigation_items
scope :all_items, -> {
includes(navigation_items: [:translations, children: :translations])
.order('navigation_items.position asc')
.where(navigation_items: { parent_id: nil })
}
end
class NavigationItem < ApplicationRecord
has_many :children, class_name: "NavigationItem", foreign_key: "parent_id"
belongs_to :parent, class_name: "NavigationItem", foreign_key: 'parent_id', optional: true
belongs_to :navigation
end
I setup the navigations in the application controller like this
def set_navigation
@navigations = Navigation.all_items
@navigation =
@footer_navigation =
@header_navigation =
if (main = @navigations.detect { |n| n.handle == "main" })
@navigation = main.navigation_items
end
if (footer = @navigations.detect { |n| n.handle == "footer" })
@footer_navigation = footer.navigation_items
end
if (header = @navigations.detect { |n| n.handle == "header" })
@header_navigation = header.navigation_items
end
end
I then loop through each navigation_item in the layout with a nested loop for the children.
All is working well except one thing. The navigation items all have a position tied to them. The parent items display in the correct order however the children are not obeying the order. Is there a way to also scope the children navigation items to order by position?
ruby-on-rails navigation sql-order-by rails-activerecord model-associations
I have a navigation structure stored in a database. There are two models Navigation and Navigation Items.
class Navigation < ApplicationRecord
has_many :navigation_items
scope :all_items, -> {
includes(navigation_items: [:translations, children: :translations])
.order('navigation_items.position asc')
.where(navigation_items: { parent_id: nil })
}
end
class NavigationItem < ApplicationRecord
has_many :children, class_name: "NavigationItem", foreign_key: "parent_id"
belongs_to :parent, class_name: "NavigationItem", foreign_key: 'parent_id', optional: true
belongs_to :navigation
end
I setup the navigations in the application controller like this
def set_navigation
@navigations = Navigation.all_items
@navigation =
@footer_navigation =
@header_navigation =
if (main = @navigations.detect { |n| n.handle == "main" })
@navigation = main.navigation_items
end
if (footer = @navigations.detect { |n| n.handle == "footer" })
@footer_navigation = footer.navigation_items
end
if (header = @navigations.detect { |n| n.handle == "header" })
@header_navigation = header.navigation_items
end
end
I then loop through each navigation_item in the layout with a nested loop for the children.
All is working well except one thing. The navigation items all have a position tied to them. The parent items display in the correct order however the children are not obeying the order. Is there a way to also scope the children navigation items to order by position?
ruby-on-rails navigation sql-order-by rails-activerecord model-associations
ruby-on-rails navigation sql-order-by rails-activerecord model-associations
asked Nov 10 at 21:17
trowse
749
749
Would adefault_scope
forNavigationItem
work? How about adding anorder
when callingnavigation_items
? I believe that theorder
for theall_items
scope only applies to the initial query forNavigation
, not to the subsequent queries for theincludes
relations.
– shanecav
Nov 10 at 23:09
I'm wrong about theorder
only applying to the initial query, at least on Rails 5.2.1. Which version of ActiveRecord are you on?
– shanecav
Nov 10 at 23:16
Hey @shanecav, also on rails 5.2.1. It is applying to the first level of navigation items but it is the sub items that are having trouble. I have tried default_scope with no luck.
– trowse
Nov 11 at 22:09
add a comment |
Would adefault_scope
forNavigationItem
work? How about adding anorder
when callingnavigation_items
? I believe that theorder
for theall_items
scope only applies to the initial query forNavigation
, not to the subsequent queries for theincludes
relations.
– shanecav
Nov 10 at 23:09
I'm wrong about theorder
only applying to the initial query, at least on Rails 5.2.1. Which version of ActiveRecord are you on?
– shanecav
Nov 10 at 23:16
Hey @shanecav, also on rails 5.2.1. It is applying to the first level of navigation items but it is the sub items that are having trouble. I have tried default_scope with no luck.
– trowse
Nov 11 at 22:09
Would a
default_scope
for NavigationItem
work? How about adding an order
when calling navigation_items
? I believe that the order
for the all_items
scope only applies to the initial query for Navigation
, not to the subsequent queries for the includes
relations.– shanecav
Nov 10 at 23:09
Would a
default_scope
for NavigationItem
work? How about adding an order
when calling navigation_items
? I believe that the order
for the all_items
scope only applies to the initial query for Navigation
, not to the subsequent queries for the includes
relations.– shanecav
Nov 10 at 23:09
I'm wrong about the
order
only applying to the initial query, at least on Rails 5.2.1. Which version of ActiveRecord are you on?– shanecav
Nov 10 at 23:16
I'm wrong about the
order
only applying to the initial query, at least on Rails 5.2.1. Which version of ActiveRecord are you on?– shanecav
Nov 10 at 23:16
Hey @shanecav, also on rails 5.2.1. It is applying to the first level of navigation items but it is the sub items that are having trouble. I have tried default_scope with no luck.
– trowse
Nov 11 at 22:09
Hey @shanecav, also on rails 5.2.1. It is applying to the first level of navigation items but it is the sub items that are having trouble. I have tried default_scope with no luck.
– trowse
Nov 11 at 22:09
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
try this
you can use default_scope
or association condition
class Navigation < ApplicationRecord
# has_many :navigation_items, -> { where(parent_id: nil).order(position: :asc) }
has_many :navigation_items, -> { where(parent_id: nil) }
scope :all_items, -> { includes(navigation_items: [children: :children]) }
end
class NavigationItem < ApplicationRecord
# has_many :children, -> { order position: :asc }, class_name: "NavigationItem", foreign_key: "parent_id"
has_many :children, class_name: "NavigationItem", foreign_key: "parent_id"
belongs_to :parent, class_name: "NavigationItem", foreign_key: 'parent_id', optional: true
belongs_to :navigation
default_scope -> { order position: :asc }
end
Thanks @PGill. Unfortunately it is still ordering the children incorrectly. I think I need to change the default order somehow to be order by parent.position then position.
– trowse
Nov 18 at 1:48
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
try this
you can use default_scope
or association condition
class Navigation < ApplicationRecord
# has_many :navigation_items, -> { where(parent_id: nil).order(position: :asc) }
has_many :navigation_items, -> { where(parent_id: nil) }
scope :all_items, -> { includes(navigation_items: [children: :children]) }
end
class NavigationItem < ApplicationRecord
# has_many :children, -> { order position: :asc }, class_name: "NavigationItem", foreign_key: "parent_id"
has_many :children, class_name: "NavigationItem", foreign_key: "parent_id"
belongs_to :parent, class_name: "NavigationItem", foreign_key: 'parent_id', optional: true
belongs_to :navigation
default_scope -> { order position: :asc }
end
Thanks @PGill. Unfortunately it is still ordering the children incorrectly. I think I need to change the default order somehow to be order by parent.position then position.
– trowse
Nov 18 at 1:48
add a comment |
up vote
0
down vote
try this
you can use default_scope
or association condition
class Navigation < ApplicationRecord
# has_many :navigation_items, -> { where(parent_id: nil).order(position: :asc) }
has_many :navigation_items, -> { where(parent_id: nil) }
scope :all_items, -> { includes(navigation_items: [children: :children]) }
end
class NavigationItem < ApplicationRecord
# has_many :children, -> { order position: :asc }, class_name: "NavigationItem", foreign_key: "parent_id"
has_many :children, class_name: "NavigationItem", foreign_key: "parent_id"
belongs_to :parent, class_name: "NavigationItem", foreign_key: 'parent_id', optional: true
belongs_to :navigation
default_scope -> { order position: :asc }
end
Thanks @PGill. Unfortunately it is still ordering the children incorrectly. I think I need to change the default order somehow to be order by parent.position then position.
– trowse
Nov 18 at 1:48
add a comment |
up vote
0
down vote
up vote
0
down vote
try this
you can use default_scope
or association condition
class Navigation < ApplicationRecord
# has_many :navigation_items, -> { where(parent_id: nil).order(position: :asc) }
has_many :navigation_items, -> { where(parent_id: nil) }
scope :all_items, -> { includes(navigation_items: [children: :children]) }
end
class NavigationItem < ApplicationRecord
# has_many :children, -> { order position: :asc }, class_name: "NavigationItem", foreign_key: "parent_id"
has_many :children, class_name: "NavigationItem", foreign_key: "parent_id"
belongs_to :parent, class_name: "NavigationItem", foreign_key: 'parent_id', optional: true
belongs_to :navigation
default_scope -> { order position: :asc }
end
try this
you can use default_scope
or association condition
class Navigation < ApplicationRecord
# has_many :navigation_items, -> { where(parent_id: nil).order(position: :asc) }
has_many :navigation_items, -> { where(parent_id: nil) }
scope :all_items, -> { includes(navigation_items: [children: :children]) }
end
class NavigationItem < ApplicationRecord
# has_many :children, -> { order position: :asc }, class_name: "NavigationItem", foreign_key: "parent_id"
has_many :children, class_name: "NavigationItem", foreign_key: "parent_id"
belongs_to :parent, class_name: "NavigationItem", foreign_key: 'parent_id', optional: true
belongs_to :navigation
default_scope -> { order position: :asc }
end
answered Nov 17 at 23:46
PGill
1,2321013
1,2321013
Thanks @PGill. Unfortunately it is still ordering the children incorrectly. I think I need to change the default order somehow to be order by parent.position then position.
– trowse
Nov 18 at 1:48
add a comment |
Thanks @PGill. Unfortunately it is still ordering the children incorrectly. I think I need to change the default order somehow to be order by parent.position then position.
– trowse
Nov 18 at 1:48
Thanks @PGill. Unfortunately it is still ordering the children incorrectly. I think I need to change the default order somehow to be order by parent.position then position.
– trowse
Nov 18 at 1:48
Thanks @PGill. Unfortunately it is still ordering the children incorrectly. I think I need to change the default order somehow to be order by parent.position then position.
– trowse
Nov 18 at 1:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243495%2fdouble-nested-includes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Would a
default_scope
forNavigationItem
work? How about adding anorder
when callingnavigation_items
? I believe that theorder
for theall_items
scope only applies to the initial query forNavigation
, not to the subsequent queries for theincludes
relations.– shanecav
Nov 10 at 23:09
I'm wrong about the
order
only applying to the initial query, at least on Rails 5.2.1. Which version of ActiveRecord are you on?– shanecav
Nov 10 at 23:16
Hey @shanecav, also on rails 5.2.1. It is applying to the first level of navigation items but it is the sub items that are having trouble. I have tried default_scope with no luck.
– trowse
Nov 11 at 22:09