sudoash
sudoash

Reputation: 73

How to Twitter.update in Rails model

I have a (cut-down) Model which looks like this:

class MyModel < ActiveRecord::Base
    def self.update_status
        event_time = time_ago_in_words 30.minutes.from_now
        Twitter.update("Event is starting in #{event_time}")
    end
end

As expected, I am getting a NoMethodError exception due to trying to use a method 'time_ago_in_words' from DateHelper. How should I accomplish this, and maybe more importantly, am I going about this the correct way?

Upvotes: 1

Views: 100

Answers (1)

Mischa
Mischa

Reputation: 43298

  1. extend ActionView::Helpers::DateHelper in your model
  2. Change 30.mins.from_now to 30.minutes.from_now

I just tried it myself and have no problem doing the following:

class MyModel < ActiveRecord::Base
  extend ActionView::Helpers::DateHelper

  def self.update_status
    event_time = time_ago_in_words(30.minutes.from_now)
    Twitter.update("Event is starting in #{event_time}")
  end
end

You have to use extend instead of include. See this article for an explanation.

Upvotes: 1

Related Questions