Ruby on rails 来自ActiveRecord的奇怪错误(可能)
我的开发环境: Ubuntu 9 Ruby 1.9.1/1.8.7 rvm 轨道2.3.5 Mysql 5.0 阿帕奇乘客 以下是代表该问题的程序流程的一部分 请求来自:Ruby on rails 来自ActiveRecord的奇怪错误(可能),ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我的开发环境: Ubuntu 9 Ruby 1.9.1/1.8.7 rvm 轨道2.3.5 Mysql 5.0 阿帕奇乘客 以下是代表该问题的程序流程的一部分 请求来自: #action def create begin @report = Report.new(params[:report]) ... rescue LocationNotFound => e ... end end 报表构造函数: class Report attr_acce
#action
def create
begin
@report = Report.new(params[:report])
...
rescue LocationNotFound => e
...
end
end
报表构造函数:
class Report
attr_accessor :locations
def initialize(params = {})
@locations = params[:locations] ? fetch_locations(params[:locations]) : []
end
...
end
取数单元位置:
def fetch_locations(loc_names)
Rails.logger.debug "LOC_NAMES: " + loc_names.inspect
ls = Location.find(:all, :conditions => [ # line 57
"locations.name in (#{loc_names.map{'?'}.join(',')})",
*loc_names
], :include => [:sample_summaries, :samples]) # loc_names will never be empty
...
end
位置模型:
class Location < ActiveRecord::Base
has_many :sample_summaries
has_many :samples, :through => :sample_summaries
...
end
我觉得很随意。有什么想法吗
另外,我还尝试将查询包装在uncached块中,但这并没有改变任何事情
编辑
以下是SampleSummary模型的外观:
class SampleSummary < ActiveRecord::Base
has_many :samples
belongs_to :location
... #validations
default_scope :include => :samples, :order => 'rss_ts desc'
...
end
编辑2
它在控制台中不会失败。那么,如果您通过./script/console多次运行控制器操作的相关部分,并使用相同的参数,它会随机失败吗?还是一直失败?不清楚 只是随机的,为什么不试试 ls=位置。查找:全部, :条件=>[locations.name in,loc_name],
:include=>:sample_summaries=>{:samples}由于sample_summaries的默认范围包括示例,您可以尝试:
def fetch_locations(loc_names)
ls = Location.find(:all,
:conditions => {:locations => {:name => loc_names},
:include => :sample_summaries
)
end
此外,如果您完全关闭:include,它是否会失败?这就是它所发生的情况:
好消息是,它只发生在config.cache_classes=false的开发模式中。将其更改为true将使其消失,但代价是每次更改后都不会忘记重新启动服务器。params[:report]可能为nil,在ReportinitializeThat logger.debug中调用nil[:locations]时出错。。。这一部分只是为了说明参数是按其应有的方式来的。正如你从日志中看到的那样,它们是,你也可以发布你的示例摘要模型吗?只有一个疑问:为什么在{locu names.map{'?'}.join','},*locu names]中使用[locations.name in,locu names],而不是[locations.name in,locu names]?仅仅因为我不知道有这样的快捷方式。谢谢!然而,这段长代码仍然是正确的。使用您的版本并没有解决此问题。Offtopic,does:conditions=>{:locations=>{:name=>loc_names}escape loc_names?关于从include中剥离样本的观点很好。这并没有帮助离开:include off将错误完全移动到另一个应该访问数据的位置(现在缺少:include)是的,无论何时使用条件的散列形式,它都会被转义。顺便说一句,它在没有条件的情况下失败的事实他:include似乎表明了一个更大的问题:include用于急切加载,减少数据库查询负载。您在没有它的情况下收到错误的事实似乎是不正确的。您提到它在您第一次启动服务器时工作,但随后的调用给您带来了错误。在没有include的情况下,这种情况仍然会发生,还是总是失败?答案是肯定的第一次以相同的方式中断ok,others-fail。感谢转义提示。使用:include=>{:sample\u summaries=>:samples}不会改变任何内容
def fetch_locations(loc_names)
ls = Location.find(:all,
:conditions => {:locations => {:name => loc_names},
:include => :sample_summaries
)
end