0
votes

I have a simple standalone model that doesn't inherit from ActiveRecord or anything else, called SmsSender. As the name suggests, it delivers text messages to an SMS gateway.

I also have an ActiveRecord model called SmsMessage which has an instance method called deliver:

def deliver
  SmsSender.deliver_message(self)
  self.update_attributes :status => "Sent"
end

The above is returning uninitialized constant SmsSender. I'm sure this is dead simple, but how can I access the SmsSender class from within my model?

1
Where is SmsSender being defined? Where is the actual file in your tree? If it's in a special directory, did you add that directory to the load path?x1a4
It's in the bog standard models path. Elsewhere in the application I access SmsSender from a controller and it works fine. This piece of functionality is developed to be used with delayed_job so I don't know if that has any impact on what is or isn't loaded?aaronrussell
Ah, silly me! I just spotted my errors. I'd actually called the class SMSSender (note capitalization) and the filename sms_sender.rb. I believe Rails is sensitive to that kind of thing. Just renamed the class to SmsSender as per my post and the class is getting loaded correctly now.aaronrussell

1 Answers

2
votes

Mabe ruby looks for SmsSender inside the current class.
Try to use the (global) scope resolution operator :: like this:

def deliver
  ::SmsSender.deliver_message(self)
  self.update_attributes :status => "Sent"
end

Also make sure the file for SmsSender is included (via one of: require, load etc)