My development environment:
- Ubuntu 9
- Ruby 1.9.1/1.8.7 (rvm)
- Rails 2.3.5
- Mysql 5.0
- Apache Passenger
Below is the part of the program flow to represent the issue.
Request comes:
#action
def create
begin
@report = Report.new(params[:report])
...
rescue LocationNotFound => e
...
end
end
Report constructor:
class Report
attr_accessor :locations
def initialize(params = {})
@locations = params[:locations] ? fetch_locations(params[:locations]) : []
end
...
end
fetch_locations:
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
Location model:
class Location < ActiveRecord::Base
has_many :sample_summaries
has_many :samples, :through => :sample_summaries
...
end
Now, the first time (after passenger restart) this runs fine and does the job. Most of the consequent times I get the error:
Mar-11 11:01:00 #15098 DEBUG: LOC_NAMES: ["Moscow, RF", "London, UK"]
Mar-11 11:01:00 #15098 DEBUG: Location Load (0.0ms) SELECT * FROM `locations` WHERE (locations.name in ('Moscow, RF','London, UK'))
Mar-11 11:01:00 #15098 DEBUG: SampleSummary Load (0.0ms) SELECT `sample_summaries`.* FROM `sample_summaries` WHERE (`sample_summaries`.location_id IN (1,3))
Mar-11 11:01:00 #15098 DEBUG: SampleSummary Columns (0.0ms) SHOW FIELDS FROM `sample_summaries`
Mar-11 11:01:00 #15098 FATAL:
NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?):
app/models/report.rb:57:in `fetch_locations'
app/models/report.rb:9:in `initialize'
app/controllers/report_controller.rb:11:in `new'
app/controllers/report_controller.rb:11:in `create'
Looks quite random to me. Any ideas?
P.S. I also tried to wrap the query in uncached
block, but that didn't change anything.
EDIT
Here is what SampleSummary model looks like:
class SampleSummary < ActiveRecord::Base
has_many :samples
belongs_to :location
... #validations
default_scope :include => :samples, :order => 'rss_ts desc'
...
end
EDIT2
It doesn't fail in console.