我正在使用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应用程序。我运行单元测试时出现以下错误。
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 :(得分: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] ['任意']”而不是“returnValue [0] ['ARBITRARY']”尽管我的查询是“SELECT'ARBITRARY'作为ARBITRARY”。 activerecord -jdbcsqlanywhere-adapter强制所有内容都是小写的。