I wrote this task to populate a table. The “Flight” table is pretty big (23000 posts). But I get this weird error when we move through the loop the second time.
Here’s the code:
desc "Compute the AirlineTerminal Table"
task :populate_airlineterminals => :environment do
#first delete content
AirlineTerminal.destroy_all
allFlights = Flight.all
#AFTER ONE ITERATION THE ERROR HAPPENS
allFlights.each do |f|
atTerminal = AirlineTerminal.where(:airline_id => f.airline_id, :airport_id => f.airport_id, :terminal => f.terminal).first
if atTerminal.nil?
atTerminal = AirlineTerminal.new
atTerminal.airport_id = f.airport_id
atTerminal.airline_id = f.airline_id
atTerminal.terminal = f.terminal
end
atTerminal.count++
atTerminal.save #so far so good
end
end
This is what happens when I run the script, after one trip through the loop it crashes.
$ rake populate_airlineterminals
rake aborted!
undefined method `+@' for true:TrueClass
Although I do not know the error my bet is that
is the culprit. As far as I know ++ is not defined in ruby. You have to use
unless of course you have defined it yourself. Maybe it causes errors while interpreting the code. Try it in irb.