这个类赋值的代码在rails v4.0.5之前是可以的 但是它从rails 4.1.x中出错了
有一个api documentation所以我猜它没有被弃用。
我不知道这是什么问题。
Loading development environment (Rails 4.1.1)
2.1.1 :001 > class Assignment < ActiveRecord::Base
2.1.1 :002?> belongs_to :group
2.1.1 :003?> has_one :event
2.1.1 :004?> accepts_nested_attributes_for :event
2.1.1 :005?> end
NameError: undefined local variable or method `generated_feature_methods' for #<Class:0x0000010a262e28>
from /Users/me/.rvm/gems/ruby-2.1.1@xxx/gems/activerecord-4.1.1/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
from /Users/me/.rvm/gems/ruby-2.1.1@xxx/gems/protected_attributes-1.0.5/lib/active_record/mass_assignment_security/nested_attributes.rb:30:in `block in accepts_nested_attributes_for'
from /Users/me/.rvm/gems/ruby-2.1.1@xxx/gems/protected_attributes-1.0.5/lib/active_record/mass_assignment_security/nested_attributes.rb:16:in `each'
from /Users/me/.rvm/gems/ruby-2.1.1@xxx/gems/protected_attributes-1.0.5/lib/active_record/mass_assignment_security/nested_attributes.rb:16:in `accepts_nested_attributes_for'
from (irb):4:in `<class:Assignment>'
from (irb):1
from /Users/me/.rvm/gems/ruby-2.1.1@xxx/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
from /Users/me/.rvm/gems/ruby-2.1.1@xxx/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
from /Users/me/.rvm/gems/ruby-2.1.1@xxx/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/me/.rvm/gems/ruby-2.1.1@xxx/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/me/.rvm/gems/ruby-2.1.1@xxx/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
在Rails 4.0.5上没问题
Loading development environment (Rails 4.0.5)
2.1.1 :001 > class Assignment < ActiveRecord::Base
2.1.1 :002?> belongs_to :group
2.1.1 :003?> has_one :event
2.1.1 :004?> accepts_nested_attributes_for :event
2.1.1 :005?> end
=> [:event]
答案 0 :(得分:7)
Rails 4.1.0.beta1 upgrade gives me undefined local variable or method generated_feature_methods可能重复。就像在那里说的那样,尝试更新protected_attributes
gem:
bundle update protected_attributes