我正在尝试从2.3.8升级到3.0.1,但是这个问题一直困扰着我并阻止了一段时间的进一步进展。任何尝试从我的模型创建的东西(例如,从控制台调用Subject.create!)总是会导致此错误:
NoMethodError: undefined method `name' for 0:Fixnum
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:26:in `block in create'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:26:in `each'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:26:in `detect'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:26:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/algebra/relations/writes.rb:24:in `call'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/session.rb:17:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/algebra/relations/relation.rb:159:in `insert'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/relation.rb:14:in `insert'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/persistence.rb:271:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/timestamp.rb:47:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/callbacks.rb:281:in `block in create'
from /.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/callbacks.rb:413:in `_run_create_callbacks'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/callbacks.rb:281:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/persistence.rb:247:in `create_or_update'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/callbacks.rb:277:in `block in create_or_update'
from /.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/callbacks.rb:413:in `_run_save_callbacks'
... 1 levels...
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/persistence.rb:56:in `save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/validations.rb:49:in `save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:242:in `block in save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:289:in `block in with_transaction_returning_status'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:204:in `transaction'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:287:in `with_transaction_returning_status'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:242:in `save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/validations.rb:34:in `create!'
from (irb):1
from /.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.1/lib/rails/commands/console.rb:44:in `start'
from /.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.1/lib/rails/commands/console.rb:8:in `start'
from /.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.1/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'ruby-1.9.2-p0
似乎Arel没有得到合适的工作关系,但我对于造成这种情况的原因感到迷茫。有什么想法吗?
谢谢!
赖安
答案 0 :(得分:0)
我建议在arel中放入一些调试语句。编辑堆栈跟踪的最后一行中的engine.rb。我想你将不得不重新启动控制台(不只是重新加载!)来查看arel文件中的更改。
这是engine.rb中的违规行:
attribute = relation.record.detect { |attr, _| attr.name.to_s == relation.primary_key.to_s }
也许尝试打印出attr名称,看看是否有任何东西通过数组检测方法传递。或者打印出“attr”的类。该类应该是“Arel :: Attribute”,但显然Fixnum会以某种方式传递给它。
attribute = relation.record.detect do |attr, _|
p attr.class
p attr.name
attr.name.to_s == relation.primary_key.to_s
end
只是一个猜测但我想也许一个插件/宝石干扰你的模型中的东西。您是否正在使用与ActiveRecord直接相关的任何依赖项?
另一项好的练习是使用一个模型创建一个空白的Rails 3应用程序(没有宝石或插件依赖项),看看是否得到相同的错误。这将排除您的开发环境中的某些内容。然后,如果可行,那么也许你可以从另一个应用程序逐个删除一些依赖项,看看其中一个是否导致错误。
答案 1 :(得分:0)
您使用的是哪个版本的Ruby?它应该是1.8.7
或1.9.2
。我与1.9.1
有类似的莫名其妙的问题。