Rails在第一次将属性分配给我的模型时抛出错误

时间:2015-01-27 22:45:23

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord

我有一个跟踪应用程序中其他模型的rails模型。 (不寻常,但这个项目是必要的)。

当我尝试分配属性model_name时,我会收到一次错误,但之后不会再出现错误。

AppModel
model_name   created_at   updated_at
----------------------------------------
User 
Question
Post
Answer     

在控制台中,我收到一个奇怪的错误:

> model = AppModel.new
> model.model_name = "User"  # First time causes the error.
> model.model_name = "User"  # Second time it works.

错误

ActiveRecord::DangerousAttributeError: record_changed? is defined by Active Record
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:104:in `instance_method_already_implemented?'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:288:in `block in define_attribute_method'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:285:in `each'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:285:in `define_attribute_method'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:252:in `block in define_attribute_methods'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:252:in `each'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:252:in `define_attribute_methods'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:75:in `block in define_attribute_methods'
    from /Users/donaldpinkus/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mutex_m.rb:73:in `synchronize'
    from /Users/donaldpinkus/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mutex_m.rb:73:in `mu_synchronize'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:72:in `define_attribute_methods'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:196:in `method_missing'
    from (irb):3
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/console.rb:90:in `start'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/console.rb:9:in `start'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `block in require'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
    from /Users/donaldpinkus/Projects/hooker/bin/rails:8:in `<top (required)>'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `block in load'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
    from /Users/donaldpinkus/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/donaldpinkus/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'2.1.5 :004 > e.model_name = "User"
 => "User"

&#34;模型名称&#34;不是模型的保留字,所以这里发生了什么?

1 个答案:

答案 0 :(得分:3)

这是因为ActiveRecord模型中已经存在一个名为model_name的方法,该方法(显然)返回此模型的名称(以及其他内容)。

  
      
  • 发件人:   ... /红宝石2.1.5 /宝石/ activemodel的-4.2.0 / lib目录/ active_model / naming.rb   @ line 222:
  •   
  • 所有者: ActiveModel :: Naming
  •   
  • 可见性:公开
  •   
  • 签名: model_name()
  •   
  • 行数: 12
  •   
     

返回模块的ActiveModel :: Name对象。它可以用来   检索各种与命名相关的信息(参见   ActiveModel :: Name以获取更多信息。)

当ActiveRecord从表中获取对象时,它还会创建名称与表中的列名对应的方法。发生的事情是它获取一个对象,在给定对象上看到一列model_name,尝试定义一个方法model_name并突然发现它已经存在。

这是一件事:避免不必要的长属性名称。如果您在模型中定义名称,则可以是name,其范围为&#34; model&#34;无论如何,班级显然是模特的名字。

但是,错误报告代码可能包含需要跟踪的错误。