2012-05-02 55 views
0

我使用的是JRuby 1.6.7,ActiveRecord 3.2.3,activerecord-jdbc-adapter 1.2.2,activerecord-jdbcsqlanywhere-adapter-1.1.1和當前的Sybase JDBC4驅動程序。使用完整的Ruby On Rails應用程序目前不是一種選擇。當我運行我的單元測試時出現以下錯誤。JRuby ActiveRecord/ActiveModel不爲表列創建方法和屬性(未定義方法)

 

    NoMethodError: undefined method `find_by_configuration_name' for # 
     method_missing at org/jruby/RubyBasicObject.java:1687 
     method_missing at /home/lynchcs/.rvm/gems/jruby-1.6.5.1/gems/activerecord-3.2.3/lib/active_record/dynamic_matchers.rb:27 

我能夠在我的單元測試中正確識別表格執行模式轉儲。這是表格的模式。



     create_table "THE_CONFIGURATION", :primary_key => "CONFIGURATION_ID", :force => true do |t| 
     t.string "CONFIGURATION_GROUP", :limit => 32 
     t.string "CONFIGURATION_TYPE", :limit => 32 
     t.string "CONFIGURATION_NAME", :limit => 32 
     t.string "CONFIGURATION_TEXT", :limit => 7168 
     end 

這是我爲它設計的模型。



    require 'rubygems' 
    require 'active_record' 

    class Configuration < ActiveRecord::Base 
     self.table_name = 'SOART_CONFIGURATION' 
     self.primary_key = 'configuration_id' 
    end 

這是我用來管理我的連接的類。該類成功連接到數據庫並執行查詢。



    require 'java' 
    require 'rubygems' 
    require 'active_record' 

    class ConnectionMaster 
     def test_object 
     "ARBITRARY" 
     end 

     def set_connection 
     ActiveRecord::Base.establish_connection(
     :adapter => 'jdbc' , 
     :driver => 'com.sybase.jdbc4.jdbc.SybDriver' , 
     :url => 'jdbc:sybase:Tds:192.168.137.137:1111/MYAPP' , 
     :username => 'noneya' , 
     :password => 'noneya' 
     ) 
     ActiveRecord::Base.connection.execute("SELECT 'ARBITRARY' AS ARBITRARY") 
     end 

     def clear_connections 
     ActiveRecord::Base.clear_active_connections! 
     end 

    end 

執行「config = Configuration.find_by_configuration_name('test')」時會出錯。如果你註釋掉這行,它會在「config.configuration_text」行中出錯。



     public void testRubyDatabaseQuery() { 
     String theValue = "test"; 
     String jrubyCode = 
     "require 'connection_master' \n" 
     + "require 'configuration' \n" 
     + "cm = ConnectionMaster.new \n" 
     + "cm.set_connection \n" 
     + "puts 'black cow of revenge' \n" 
     + "config = Configuration.find(:all,:conditions => ['configuration_name=?','test']) \n" 
     + "config = Configuration.find_by_configuration_name('test') \n" 
     + "puts 'red cow of revenge' \n" 
     + "cm.clear_connections \n" 
     + "config.configuration_text \n"; 
     String executeValue = (String)JRubyMaster.execute(jrubyCode); 
     this.assertEquals("executeValue and theValue do not match.", theValue, executeValue); 
     } 

回答

0

雖然構建的問題,我發現我自己的答案:)我覺得這是足夠有趣的,去提出問題和答案。

在我的第一次努力中,我使用了activerecord-jdbc-adapter,問題在於我使用的是「Configuration.find_by_configuration_name」,它的內容是「Configuration.find_by_CONFIGURATION_NAME」,與「config.configuration_text」相同。數據庫中大寫的列名和activerecord-jdbc-adapter使用大寫形式創建方法。一旦發生這種變化,就會由於使用「LIMIT」而開始拋出和無效的sql錯誤。

然後我切換到使用activerecord-jdbcsqlanywhere-adapter,它使用「Configuration.find_by_configuration_name」和「config.configuration_text」,但單元測試失敗。這是因爲它構建了「returnValue [0] ['arbitrary']」而不是「returnValue [0] ['ARBITRARY']」儘管我的查詢是「SELECT'ARBITRARY'as ARBITRARY」。 activerecord-jdbcsqlanywhere-adapter強制所有內容都爲小寫。

+0

將此答案標記爲您接受的答案(綠色複選標記) –