Multiple nested set in same model
vill opened this issue · 1 comments
vill commented
Hi there, for example I have such models:
# == Schema Information
#
# Table name: companies
#
# id :integer not null, primary key
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
#
class Company < ApplicationRecord
has_many :products
end
# == Schema Information
#
# Table name: products
#
# id :integer not null, primary key
# name :string not null
# price :float default(0.0), not null
# company_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# parent_id :string
# lft :integer
# rgt :integer
# depth :integer default(0), not null
# children_count :integer default(0), not null
# vendor_code :string not null
# external_id :string not null
#
# Indexes
#
# index_products_on_company_id_and_external_id (company_id,external_id) UNIQUE
# index_products_on_depth (depth)
# index_products_on_lft (lft)
# index_products_on_name_and_company_id (name,company_id)
# index_products_on_parent_id (parent_id)
# index_products_on_rgt (rgt)
# index_products_on_vendor_code (vendor_code)
#
# Foreign Keys
#
# fk_rails_159ee3fc63 (company_id => companies.id)
#
class Product < ApplicationRecord
belongs_to :company
end
How it is possible to make correctly that awesome_nested_set
in the searches limited search on company_id
in the Product
model?
The available parameters for children
configuration are:
has_many_children_options = {
:class_name => self.base_class.to_s,
:foreign_key => parent_column_name,
:primary_key => primary_column_name,
:inverse_of => (:parent unless acts_as_nested_set_options[:polymorphic]),
}
has_many :children, -> { order(quoted_order_column_full_name) }, has_many_children_options
The available parameters for parent
configuration are:
options = {
:class_name => self.base_class.to_s,
:foreign_key => parent_column_name,
:primary_key => primary_column_name,
:counter_cache => acts_as_nested_set_options[:counter_cache],
:inverse_of => (:children unless acts_as_nested_set_options[:polymorphic]),
:polymorphic => acts_as_nested_set_options[:polymorphic],
:touch => acts_as_nested_set_options[:touch]
}
options[:optional] = true if ActiveRecord::VERSION::MAJOR >= 5
belongs_to :parent, options
To solve my problem, I have to do so in the Product
model:
class Product < ApplicationRecord
belongs_to :company
acts_as_nested_set primary_column: :external_id, scope: :company_id
# Override relationships for acts_as_nested_set
belongs_to :parent, ->(child) { where company_id: child.company_id },
primary_key: :external_id,
foreign_key: :parent_id,
inverse_of: :children,
optional: true
has_many :children, ->(parent) { where company_id: parent.company_id },
primary_key: :external_id,
foreign_key: :parent_id,
inverse_of: :parent
end
Is it possible to do it more correctly?
stale commented
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.