1

这是我的测试模型代码:

require 'rubygems'
require 'composite_primary_keys'
class VamTest < ActiveRecord::Base
  set_table_name 'tests'
  set_primary_keys :service, :host
end

这是我在运行以下命令时得到的堆栈跟踪:

>> VamTest.all
VamTest.all
ArgumentError: wrong # of arguments(0 for 1)
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1:in `class_eval'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2232:in `compute_type'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2230:in `compute_type'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1665:in `instantiate'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:664:in `find_by_sql'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:664:in `collect!'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:664:in `find_by_sql'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1578:in `find_every'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:618:in `find'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:638:in `all'
    from (irb):2
>> 

查看以下内容:

>> VamTest.find(:first)
VamTest.find(:first)
ArgumentError: wrong # of arguments(0 for 2)
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1:in `class_eval'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2232:in `compute_type'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2230:in `compute_type'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1665:in `instantiate'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:664:in `find_by_sql'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:664:in `collect!'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:664:in `find_by_sql'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1578:in `find_every'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1535:in `find_initial'
    from C:/jruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:616:in `find'
    from (irb):31
>>

我觉得我的模型中的主键定义存在问题,但我无法弄清楚。

4

1 回答 1

0

我怀疑命名你的模型Test是这里的麻烦之源。打开控制台并输入Test. 如果它真的在找到你的模型,你会看到类似这样的东西。

=> Testid: integer, service: string, host: string, created_at: datetime, updated_at: datetime

虽然,我猜你会看到这个:

=> Test

将您的一次性模型更改为类似的东西Foo,看看效果如何。

于 2010-08-24T16:36:18.610 回答