rails不允许我从另一个方法内部调用方法 - rails一直说这个方法是未定义的

时间:2012-06-19 00:59:51

标签: ruby-on-rails methods rails-models

在我的辅导课程模型中,我有一些代码可以在不同时间触发提醒文本。一切正常,直到我尝试了一些重构,现在我遇到了问题。

def send_reminder_text(texts_batch)
    texts_batch.each do |text|
        page_number = Refugee.find(text.refugee_id)[:last_page]
        body_of_text = text[:begin_time].in_time_zone.strftime("Burma Reminder: upcoming session at %I:%M%p beginning on page #{page_number}. 
            Please email jek2141@columbia.edu to reschedule or cancel the session.") 
        text.begin_text(body_of_text)
    end
end

def self.deliver_pm_reminder_text
    texts_batch = TutoringSession.batch_for_pm_reminder_text
    send_reminder_text(texts_batch)
end

def self.deliver_just_before_reminder_text
    texts_batch = TutoringSession.batch_for_just_before_reminder_text
    send_reminder_text(texts_batch)
end

当我调用deliver_just_before_reminder_text函数时,收到以下错误消息:

irb(main):006:0> TutoringSession.send_reminder_text
NoMethodError: undefined method `send_reminder_text' for #<Class:0x00000003cfe5d8>
    from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record   /base.rb:1088:in `method_missing'
from (irb):6
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

尽管上面已明确定义了send_reminder_text,但仍然存在这一点。

1 个答案:

答案 0 :(得分:3)

您的方法声明指定该方法位于TutoringSession类的对象:

def send_reminder_text(texts_batch)

但是你试图将它称为类本身上的方法:

irb(main):006:0> TutoringSession.send_reminder_text

尝试将您的定义更改为:

def self.send_reminder_text(texts_batch)