简体   繁体   中英

Rails - Accessing model class methods from within ActiveRecord model

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM