Вопрос

I got a problem. I use whenever to run daily a method in a model class (of course). This method iterates over a collection of "holidays" instances and then notifies the existence of this holiday to a client (another model).

Here comes the problem; the holidays instances have an attribute called "notified". I use this to know if that holidays was notified to the clients (I don't want to notify a holiday twice). In order to do that, i need to access to the attributes of the holiday instance to chango the value (boolean) of the attribute "notified". But I can not do that (i get no errors, but the attribute doesn't get updated -always false-)

Any hints?

holidays.rb

class Holiday < ActiveRecord::Base
  belongs_to :user

  def self.notify
    @holidays = Holiday.all

    @today = DateTime.now.to_date

    @holidays.each do |holiday|
      if holiday.notified == false && (holiday.fecha - @today).to_i < 15
        @clients = holiday.user.clients

        @clients.each do |client|
          ReminderMailer.new_holiday_reminder(holiday, client).deliver
        end
        holiday.notified = true <== I need to include something like this

      end
    end
  end

end

And the scheduler.rb

every :day do
  runner "Holiday.notify", :environment => 'development'
end

THANKS!!

Это было полезно?

Решение

Use update_attribute. Also, You don't have to use instance variables in your notify method

class Holiday < ActiveRecord::Base
  belongs_to :user

  def self.notify
    holidays = Holiday.all
    today = DateTime.now.to_date
    holidays.each do |holiday|
      if holiday.notified == false && (holiday.fecha - today).to_i < 15
        clients = holiday.user.clients
        clients.each do |client|
          ReminderMailer.new_holiday_reminder(holiday, client).deliver
        end
        holiday.update_attribute(:notified, true) #or holiday.update_attributes(:notified => true)
      end
    end
  end
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top