很棒的 nestset set ancestor class 方法
Awesome nestset set ancestor class method
很棒的嵌套集包括一个祖先实例方法:
https://github.com/collectiveidea/awesome_nested_set/wiki/Awesome-nested-set-cheat-sheet
@john = Group.where(name: "John").first
@tree = @john.ancestors
我正在寻找一种 class 方法,该方法将 return 每个名为 "John"
的组的祖先的数组或 AR 关系
@johns = Group.where(name: "John")
@tree = @johns.ancestors
目前我正在通过循环 AR 关系和 运行 每行的实例方法来做到这一点。
更新1
class Group < ApplicationRecord
acts_as_nested_set :counter_cache => :children_count
def self.build_tree(groups)
groups.collect(&:ancestors).flatten!
end
end
class GroupsController < ApplicationController
def index
@johns = Group.where(name: "John")
@tree = Group.build_tree(@johns)
end
end
错误:
undefined method `collect' for #<Class:0x00000002a28378>
更新2
Ancestor => Group 关系似乎有问题。
class Group < ApplicationRecord
acts_as_nested_set :counter_cache => :children_count
has_many :ancestors
def self.build_tree(objects)
objects.collect(&:ancestors).flatten!
end
End
class Ancestor < ActiveRecord::Base
belongs_to :group
scope :with_group, -> (name) { joins(:group).where("groups.name = ?", name) }
end
2.4.0 :008 > Ancestor.joins(:group)
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "ancestors" does not exist
LINE 1: SELECT "ancestors".* FROM "ancestors" INNER JOIN "groups" O...
^
: SELECT "ancestors".* FROM "ancestors" INNER JOIN "groups" ON "groups"."id" = "ancestors"."group_id" LIMIT
2.4.0 :009 > Ancestor.includes(:group)
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "ancestors" does not exist
LINE 1: SELECT "ancestors".* FROM "ancestors" LIMIT
^
: SELECT "ancestors".* FROM "ancestors" LIMIT
你快到了,试试关注
@johns = People.where(name: "John")
@tree = @johns.collect(&:ancestors).flatten!
或者您可以使用连接查询
Ancestor.joins(:people).where("peoples.name = ?", 'John')
根据您的代码库,您可以只传递记录,但这不是一个好方法。
class People < ActiveRecord::Base
acts_as_nested_set :counter_cache => :children_count
def self.build_tree(peoples)
peoples.collect(&:ancestors).flatten!
end
end
class PeopleController < ApplicationController
def index
@johns = People.where(name: "John")
@tree = People.build_tree(@johns)
end
end
示例scope
,很好
class People < ActiveRecord::Base
has_many :ancestors
#your codes goes here
end
class Ancestor < ActiveRecord::Base
belongs_to :people
scope :with_people, -> (name) { joins(:people).where("peoples.name = ?", name) }
end
class PeopleController < ApplicationController
def index
@tree = Ancestor.with_people("John")
end
end
很棒的嵌套集包括一个祖先实例方法: https://github.com/collectiveidea/awesome_nested_set/wiki/Awesome-nested-set-cheat-sheet
@john = Group.where(name: "John").first
@tree = @john.ancestors
我正在寻找一种 class 方法,该方法将 return 每个名为 "John"
的组的祖先的数组或 AR 关系@johns = Group.where(name: "John")
@tree = @johns.ancestors
目前我正在通过循环 AR 关系和 运行 每行的实例方法来做到这一点。
更新1
class Group < ApplicationRecord
acts_as_nested_set :counter_cache => :children_count
def self.build_tree(groups)
groups.collect(&:ancestors).flatten!
end
end
class GroupsController < ApplicationController
def index
@johns = Group.where(name: "John")
@tree = Group.build_tree(@johns)
end
end
错误:
undefined method `collect' for #<Class:0x00000002a28378>
更新2
Ancestor => Group 关系似乎有问题。
class Group < ApplicationRecord
acts_as_nested_set :counter_cache => :children_count
has_many :ancestors
def self.build_tree(objects)
objects.collect(&:ancestors).flatten!
end
End
class Ancestor < ActiveRecord::Base
belongs_to :group
scope :with_group, -> (name) { joins(:group).where("groups.name = ?", name) }
end
2.4.0 :008 > Ancestor.joins(:group)
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "ancestors" does not exist
LINE 1: SELECT "ancestors".* FROM "ancestors" INNER JOIN "groups" O...
^
: SELECT "ancestors".* FROM "ancestors" INNER JOIN "groups" ON "groups"."id" = "ancestors"."group_id" LIMIT
2.4.0 :009 > Ancestor.includes(:group)
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "ancestors" does not exist
LINE 1: SELECT "ancestors".* FROM "ancestors" LIMIT
^
: SELECT "ancestors".* FROM "ancestors" LIMIT
你快到了,试试关注
@johns = People.where(name: "John")
@tree = @johns.collect(&:ancestors).flatten!
或者您可以使用连接查询
Ancestor.joins(:people).where("peoples.name = ?", 'John')
根据您的代码库,您可以只传递记录,但这不是一个好方法。
class People < ActiveRecord::Base
acts_as_nested_set :counter_cache => :children_count
def self.build_tree(peoples)
peoples.collect(&:ancestors).flatten!
end
end
class PeopleController < ApplicationController
def index
@johns = People.where(name: "John")
@tree = People.build_tree(@johns)
end
end
示例scope
,很好
class People < ActiveRecord::Base
has_many :ancestors
#your codes goes here
end
class Ancestor < ActiveRecord::Base
belongs_to :people
scope :with_people, -> (name) { joins(:people).where("peoples.name = ?", name) }
end
class PeopleController < ApplicationController
def index
@tree = Ancestor.with_people("John")
end
end