新手问题 ancestry 问题 (已解决)

i5ting · 2013年02月20日 · 最后由 i5ting 回复于 2013年02月20日 · 2771 次阅读

目前只看到 源码 https://github.com/stefankroes/ancestry

2 年前的 demo,我的代码与之类似,跑不起来 http://railscasts.com/episodes/262-trees-with-ancestry

 git:(master)  rails c
Loading development environment (Rails 3.2.11)
irb(main):001:0> Category.create! :name => 'Stinky', :parent => Category.create!(:name => 'Squeeky')
   (0.1ms)  begin transaction
  SQL (51.1ms)  INSERT INTO "categories" ("ancestry", "created_at", "desc", "name", "updated_at") VALUES (?, ?, ?, ?, ?)  [["ancestry", nil], ["created_at", Wed, 20 Feb 2013 03:11:29 UTC +00:00], ["desc", nil], ["name", "Squeeky"], ["updated_at", Wed, 20 Feb 2013 03:11:29 UTC +00:00]]
   (1.1ms)  commit transaction
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: parent
    from /usr/local/lib/ruby/gems/1.9.1/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
    from /usr/local/lib/ruby/gems/1.9.1/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
    from /usr/local/lib/ruby/gems/1.9.1/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
    from /usr/local/lib/ruby/gems/1.9.1/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
    from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
    from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/base.rb:497:in `initialize'
    from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/validations.rb:39:in `new'
    from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/validations.rb:39:in `create!'
    from (irb):1
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
irb(main):002:0> message = Message.new(:parent_id =>'1')
NameError: uninitialized constant Message
    from (irb):2
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
irb(main):003:0> 

还有别的资料么?


class Category < ActiveRecord::Base
  attr_accessible :desc, :name 
  has_ancestry
end

已经增加了 has_ancestry,但是无法访问 parent,

class Category < ActiveRecord::Base
  attr_accessible :desc, :name, :parent
  has_ancestry
end

我们的代码里好像有 parent,你可以试试

#2 楼 @williamherry 谢谢你啊,我之前加了的,但是可能 rails c 没有重载

需要 登录 后方可回复, 如果你还没有账号请 注册新账号