[Rails 4] 你好。我正在创建一个 web 应用程序。这与 Rails 4 一起使用。此外,该项目未使用 ActiveRelations 或数据库。然而,它正在使用一个预先存在的 M$_SQL DB,我使用我发现的名为 tiny_tds 的 gem 进行查询。
我遇到了一个错误,我非常坚持。
基本上,我的模型是一个“网络”类,看起来像这样:
应用程序/模型/network.rb
class Network
include Query #THIS IS MY CONCERN MODULE (SHOWN BELOW)
include ActiveModel::Conversion
include ActiveModel::Validations
extend ActiveModel::Naming
...
def initialize()
@technology = "CDMA"
@sites = []
end
...
def get_sites()
#Custom query - this calls the Query Module
@sites = Query.query_avg(@technology, @s_range, @e_range)
end
...
end
此类调用我创建并停留在 /concerns 文件夹中的模块。它依赖于“tiny_tds”gem,这是一个用于查询 M$_SQL DB 的 gem。在我尝试将它与 Rails 集成之前,它一直运行得非常好:
应用程序/模型/关注/query.rb
module Query
extend ActiveSupport::Concern
def self.query_avg(tech, s_date, e_date)
q_string = "..." #custom sql string (omitted for brevity)
return execute(q_string) #return to calling class
end
private
def self.execute(sql)
#This is a TinyTds Specific command (where error is seen)
client = TinyTds::Client.new(username: '...', password: '...', host: 'x.x.x.x')
result = client.execute(sql)
results = result.each(:symbolize_keys => true, :as => :array, :cache_rows => true, :empty_sets => true) do |rowset| end
return results
end
end
最后但同样重要的是,这里是调用代码的控制器:
应用程序/控制器/networks_controller.rb
class NetworksController < ApplicationController
def new
@network = Network.new
d1 = Date.new(2014,11,22)
d2 = Date.new(2014,11,30)
@network.date_range(d1,d2)
@network.get_sites
end
现在......当我加载“rails console”并在那里手动输入时,上面的代码(在控制器中)工作得很好。所有的数据正是我想要的。
但是,当我尝试调用http://localhost:3000/networks/new(即使视图为空)时,我在浏览器中收到以下错误:
uninitialized constant Query::TinyTds
...(inside the query.rb module listed above)...
client = TinyTds::Client.new(username: '...', password: '...', host: 'x.x.x.x')
PS。我 rand bundle install 并验证了 tiny_tds gem 已安装。
感谢所有帮助,谢谢!