Rails NoMethodError in loop when method exists

Posted by Kevin Whitaker on Stack Overflow See other posts from Stack Overflow or by Kevin Whitaker
Published on 2010-04-16T16:07:29Z Indexed on 2010/04/16 16:13 UTC
Read the original article Hit count: 311

Filed under:
|
|

Good day all.

I'm running into a bit of a problem getting a script running on my production environment, even though it works just fine on my dev box. I've verified that all the requisite gems and such are the same version.

I should mention that the script is intended to be run with the script/runner command.

Here is a super-condensed version of what I'm trying to do, centered around the part that's broken:


def currentDeal
 marketTime = self.convertToTimeZone(Time.new)
 deal = Deal.find(:first, :conditions => ["start_time  ? AND market_id = ? AND published = ?", marketTime, marketTime, self.id, 1])
 return deal
end

markets = Market.find(all)
markets.each do |market|
  deal = market.currentDeal
  puts deal.subject
end

Now convertToTimeZone is a method attached to the model. So, this code works just fine on my dev machine, as stated. However, attempting to run it on my production machine results in:


undefined method `subject' for nil:NilClass (NoMethodError)

If, however, I go into the console on the production box and do this:


def currentDeal
  marketTime = self.convertToTimeZone(Time.new)
  deal = Deal.find(:first, :conditions => ["start_time  ? AND market_id = ? AND published = ?", marketTime, marketTime, self.id, 1])
  return deal
end

market = Market.find(1)
deal = market.currentDeal
puts deal.subject

It returns the correct value, no problem. So what is going on?

This is on rails v 2.3.5, on both machines.

Thanks for any help

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about methods