我用祖先制作一个目标树.我想使用json将该树的内容发送到浏览器.
我的控制器是这样的:
@goals = Goal.arrange
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @goals }
format.json { render :json => @goals}
end
Run Code Online (Sandbox Code Playgroud)
当我打开json文件时,我得到了这个输出:
{"#<Goal:0x7f8664332088>":{"#<Goal:0x7f86643313b8>":{"#<Goal:0x7f8664331048>":{"#<Goal:0x7f8664330c10>":{}},"#<Goal:0x7f8664330e68>":{}},"#<Goal:0x7f86643311b0>":{}},"#<Goal:0x7f8664331f70>":{},"#<Goal:0x7f8664331d18>":{},"#<Goal:0x7f8664331bd8>":{},"#<Goal:0x7f8664331a20>":{},"#<Goal:0x7f86643318e0>":{},"#<Goal:0x7f8664331750>":{},"#<Goal:0x7f8664331548>":{"#<Goal:0x7f8664330aa8>":{}}}
Run Code Online (Sandbox Code Playgroud)
如何在json文件中呈现Goal-objects的内容?
我试过这个:
@goals.map! {|goal| {:id => goal.id.to_s}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为@goals是一个有序的哈希.
我在Heroku上看到的问题只有祖先宝石(无法在本地重现):https: //github.com/stefankroes/ancestry
home#upload中出现NoMethodError:#的未定义方法`ancestry'
方法如下所示:
def anthem_upload
user = User.find_by(id: params[:user_id])
anthem = Anthem.create!(user_id: user.id, body:params[:post]) <-- HAPPENS HERE
...
ene
Run Code Online (Sandbox Code Playgroud)
此方法甚至不会调用.ancestry方法 - 但只要模型创建就会发生异常.
模型看起来像这样:
class Anthem < ActiveRecord::Base
has_ancestry
...
end
Run Code Online (Sandbox Code Playgroud)
这是日志:
Run Code Online (Sandbox Code Playgroud)A NoMethodError occurred in home#anthem_upload: undefined method `ancestry' for #<Anthem:0x007f1684d58b98> app/controllers/home_controller.rb:335:in `anthem_upload'
我的任务是建立类似于此的东西,我需要一些建议/意见来获得有效的解决方案:
到目前为止,我已尝试使用嵌套集模型来表示我的数据,但我认为不可能有多个父节点.我见过的几乎所有例子都只涉及一个有多个孩子的父母.我也尝试使用Google Visualization Organization Chart,它看起来很棒,几乎解决了所有显示问题,但我遇到的问题是节点只支持1个父节点.所以我想我尝试自己建造一些东西,我没有太多运气.
至于正确显示树,最大的问题之一是弄清楚如何将节点放在正确的位置.请注意,在Family Echo中,父节点根据其拥有的子节点间隔正确,以免与其他周围节点发生冲突.我一直试图至少复制这种行为,所以我非常感谢任何提示.我尝试循环遍历下面的数组,其中包含有关该系列的所有数据:
{
508 : {
"id" : 1,
"name" : "Son",
"Parent" : {
17864 : {
"id" : 2,
"name" : "Father"
},
65926 : {
"id" : 3,
"name" : "Mother"
}
}
},
17864 : {
"id" : 2,
"name" : "Father",
"Partner" : {
65926: {
"id" : 3,
"name" : "Mother"
}
},
"Son/Daughter" : {
508 : {
"id" : 1,
"name" : "Son"
}
}
}, …
Run Code Online (Sandbox Code Playgroud) 我有一个属于类别的条目模型。类别模型使用祖先,以便我可以嵌套类别。
我想按其根类别对所选条目进行分组。我遇到的问题是,为了做到这一点,rails 对每个条目执行查询以获取根类别。因此,如果我有 20 条记录,它将执行 20 个查询以按根类别分组。
我已经通过预先加载类别(如下所示)减少了查询数量,但我不知道如何预先加载类别的根。
这是我正在使用的代码:
控制器
@entries = current_user.entries.includes(:category)
@entries_by_category = @entries.group_by { |entry| entry.category.root }
Run Code Online (Sandbox Code Playgroud)
入口.rb
class Entry < ActiveRecord::Base
belongs_to :category
end
Run Code Online (Sandbox Code Playgroud)
类别.rb
class Category < ActiveRecord::Base
has_ancestry
has_many :entries
end
Run Code Online (Sandbox Code Playgroud)
我试图把默认范围的分类模型,像这样:default_scope { includes(:ancestry) }
。但这并没有改变执行查询的数量。而且我无法弄清楚如何includes()
在原始查询上使用以包含类别和类别的根。
作为一些额外的信息,类别只能嵌套 2 层深(仅类别和子类别)。所以 root 在技术上意味着父级,它不必遍历多个级别。
有任何想法吗?
在控制台中:
Course.ids.count
=> 1766
Course.pluck(:id).count
=> 1766
Course.ids.uniq.count
=> 1529
Course.count
=> 1529
Run Code Online (Sandbox Code Playgroud)
这是正常的?
小评论 - 模型课程使用祖先(宝石).
UPD1:
生成的sql:
Learn::Course.ids.count
(5.4ms) SELECT "learn_courses"."id" FROM "learn_courses" LEFT OUTER JOIN "learn_course_translations" ON "learn_course_translations"."learn_course_id" = "learn_courses"."id"
=> 1766
Learn::Course.count
(1.5ms) SELECT COUNT(*) FROM "learn_courses"
=> 1529
Run Code Online (Sandbox Code Playgroud)
嗯...
UPD2:
架构信息
#
# Table name: learn_courses
#
# id :integer not null, primary key
# name :string(255)
# position :integer
# created_at :datetime
# updated_at :datetime
# ancestry :string(255)
# course_type :string(255)
# article :string(255) …
Run Code Online (Sandbox Code Playgroud) 我正在使用Ancestry在Rails中构建一个分层类别,我允许用户选择他们正在创建的对象的父级.我使用下拉列表显示现有类别:
<%= f.input :ancestry, collection: Category.sorted_list %>
Run Code Online (Sandbox Code Playgroud)
只要用户选择现有节点,一切都很好.如果用户在下拉列表中选择空白,我希望Ancestry创建一个根节点,但是表单会抛出"无效"错误.
我的控制器没有做任何令人费解的事情:
def update
@category = Category.find(params[:id])
respond_to do |format|
if @category.update_attributes(params[:category])
format.html { redirect_to @category, notice: 'Category was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
Run Code Online (Sandbox Code Playgroud)
我是Rails的新手,所以我不确定如何解决这个问题.我错过了Ancestry的配置,还是这个表格验证器可能过度保护?
只提到GitHub文档中的Rails 3 - 有人知道它是否与Rails 4兼容?
我问,因为看起来它已被更新以处理Ruby 2.1.0
我想运行一些 RSpec 测试来测试 Rails 中的 Ancestry。我想使用 FactoryGirl 为测试创建机构实例。我尝试了下面提到的以下方法,但没有成功创建子记录。
工厂.rb文件
factory :institution do
# sequence(:name) { |n| "University of XYZ#{n}" }
end
Run Code Online (Sandbox Code Playgroud)
在测试中,我使用以下样式 样式:1
let(:institution){ FactoryGirl.create(:institution, name: "ABC Institution") }
let(:institution_child) {FactoryGirl.create(:institution, name: "Sub Institution", ancestry: institution.id) }
Run Code Online (Sandbox Code Playgroud)
款式:2
let(:institution){ FactoryGirl.create(:institution, name: "ABC Institution") }
let(:institution_child) {FactoryGirl.create(:institution, name: "Sub Institution", parent_id: institution.id) }
Run Code Online (Sandbox Code Playgroud)
款式:3
describe Institution do
before do
@institution = Institution.create({:institution, name: "ABC Institution"})
@institution_child = Institution.create(:institution, name: "Sub Institution"}
end
it "should create the child institution" do
assigns(:institution_child).should be_a_new(Institution) …
Run Code Online (Sandbox Code Playgroud) 我想使用content_tag呈现下面显示的结构,其中集合是祖先对象.
<ul>
<li>
<a>Fruits</a>
<ul>
<li>
<a>Apple</a>
</li>
<li>
<a>Orange</a>
</li>
</ul>
</li>
<li>
<a>Colours</a>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud) 我的应用程序中有祖先树.
我想仅在同一级别上实现记录的唯一性.
让我解释
记录:
#<Folder id: 1, title: "folder1", ... ancestry: nil>,
#<Folder id: 2, title: "folder2", ... ancestry: "1">,
#<Folder id: 3, title: "folder3", ... ancestry: "1/2">
Run Code Online (Sandbox Code Playgroud)
如果我想创建folder (:title => 'folder2', :ancestry => '1')
它应该引发错误title is olready taken
,
但如果我创建 folder (:title => 'folder2', :ancestry => nil or "1/2")
它应该创建新的记录.
可能有人有这个问题,可以给出建议或答案
(在祖先方法中@ elem.siblings返回@elem级别的所有记录)
我有两个型号:
Post
- category_id
Category (ancestry model)
Run Code Online (Sandbox Code Playgroud)
类别树看起来像这样的例子:
- Root Category
- Sub Category
Run Code Online (Sandbox Code Playgroud)
现在让我们说帖子被归类为子类别.我在根类别中,我想看到所有在子类别中的帖子.是否有可能与祖先一起得到这些?
类别树总是只有一个嵌套级别,所以也许祖先太多了..
提前致谢
只有一个嵌套级别的工作示例
@category = Category.find(params[:id])
category_ids = @category.children.map(&:id) << @category.id
category_ids = category_ids.join(",")
@posts = Post.recent.where("category_id IN (#{category_ids})").page(params[:page])
Run Code Online (Sandbox Code Playgroud) ancestry ×11
activerecord ×2
content-tag ×1
css ×1
factory-bot ×1
group-by ×1
hierarchy ×1
html ×1
json ×1
php ×1
rspec ×1
ruby ×1