Rails 3,sqlserver,使用set_primary_key,无法使用特定ID查找

时间:2011-07-13 16:55:32

标签: sql-server ruby-on-rails-3 odbc

我正在尝试将Rails2应用程序移植到Rails3 - 这个应用程序为多个数据库提供了一个前端,其中一个是SQL Server 2005. SQL Server 2005数据库是遗留的,所以我必须使用set_table_name和set_primary_key就这样:

class Project < ActiveRecord::Base
  set_table_name "SpiraTest.TST_PROJECT"
  set_primary_key "PROJECT_ID"
end

启动控制台我可以看到

irb(main):002:0> Project.primary_key
=> "PROJECT_ID"
irb(main):003:0> Project.table_name
=> "SpiraTest.TST_PROJECT"
irb(main):004:0> 

我甚至可以使用Project.find(:first),Project.find(:last)等来检索数据。我不能做的是找到一个id,如下所示:Project.find(1)。我遇到了:

NoMethodError: undefined method `eq' for nil:NilClass
        from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.9/lib/active_support/whiny_nil.rb:48:in `method_missing'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:317:in `find_one'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:292:in `find_with_ids'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:107:in `find'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/active_record/base.rb:444:in `__send__'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/active_record/base.rb:444:in `find'
        from (irb):4
irb(main):005:0> 

我一直在查看finder_methods并且可以看到find_one方法中的哪个

  record = where(primary_key.eq(id)).first

'primary_key'是零。

我对此感到茫然 - 我会继续打猎,看看我发现了什么。

用于完整性的宝石:

gem 'rails', '3.0.9'
gem 'mysql2', '< 0.3'
gem 'activerecord-sqlserver-adapter'
gem 'mongrel'
gem 'psrutil'
gem 'net-ldap'
gem 'spreadsheet'
gem 'ruby-odbc'

1 个答案:

答案 0 :(得分:0)

确保使用set_primary_key并注意,它区分大小写。