简体   繁体   English

Rails-从ActiveRecord模型中访问模型类方法

[英]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 . 我有一个简单的独立模型,它不继承自ActiveRecord或其他任何东西,称为SmsSender As the name suggests, it delivers text messages to an SMS gateway. 顾名思义,它将文本消息传递到SMS网关。

I also have an ActiveRecord model called SmsMessage which has an instance method called deliver : 我也有称为ActiveRecord模型SmsMessage其中有所谓的实例方法deliver

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

The above is returning uninitialized constant SmsSender . 上面的返回uninitialized constant SmsSender I'm sure this is dead simple, but how can I access the SmsSender class from within my model? 我确定这很简单,但是如何从模型中访问SmsSender类呢?

Mabe ruby looks for SmsSender inside the current class. Mabe ruby​​在当前类中寻找SmsSender
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) 还要确保包含SmsSender的文件(通过以下方式之一:要求,加载等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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