向表添加值 - ActiveRecord :: DangerousAttributeError:update由ActiveRecord定义

时间:2012-11-14 18:17:50

标签: ruby-on-rails ruby

好吧伙计们我有一个问题 - 因为我创建了一个模型和表管理员用户,然后我尝试添加值,然后控制台打印我...任何想法?

1.9.3-p286 :020 > AdminUser
 => AdminUser(id: integer, first_name: string, last_name: string, email: string, pass: string, date_reg: datetime, update: datetime) 

1.9.3-p286 :021 > AdminUser.all
  AdminUser Load (0.2ms)  SELECT "admin_user".* FROM "admin_user" 
 => [] 

1.9.3-p286 :022 > me =  AdminUser
 => AdminUser(id: integer, first_name: string, last_name: string, email: string, pass: string, date_reg: datetime, update: datetime) 
1.9.3-p286 :023 > me.new
ActiveRecord::DangerousAttributeError: update is defined by ActiveRecord
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:91:in `instance_method_already_implemented?'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:263:in `block in define_attribute_method'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:260:in `each'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:260:in `define_attribute_method'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:256:in `block in define_attribute_methods'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:256:in `each'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:256:in `define_attribute_methods'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:66:in `block in define_attribute_methods'
    from <internal:prelude>:10:in `synchronize'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:63:in `define_attribute_methods'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:168:in `respond_to?'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:398:in `__run_callback'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_initialize_callbacks'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/base.rb:501:in `initialize'
    from (irb):23:in `new'
    from (irb):23
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
    from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'1.9.3-p286 :024 > 

1 个答案:

答案 0 :(得分:3)

您的问题是update是模型中的一列。但更新也是Active Record定义的方法。因此,如果您尝试访问admin.update,则rails不知道您在谈论哪个。如果您将update列重命名为其他内容,则应该没问题。

您可能希望查看时间戳。 http://guides.rubyonrails.org/migrations.html#special-helpers我认为您希望使用update列进行操作。