¿Actualizar registros de forma inminente después de la acción del controlador de rieles?

My app has activity notifications, each has a flag 'seen' which I use to check if the user has looked at them.

Mi controlador se ve así:

def index
  @unseen_activities = current_user.notifications.unseen.order(:updated_at => :desc)
  @seen_activities = current_user.notifications.seen.order(:updated_at => :desc)

  # mark them as viewed
  current_user.notifications.update_all(:seen => true)
end

But the activities are all loaded as having been seen, even though this is updated after the collections are made. What am I missing? #noob

preguntado el 12 de septiembre de 13 a las 01:09

Has confirmado que current_user.notifications.unseen.order(:updated_at => :desc) does indeed include some unseen activities? -

1 Respuestas

Try putting the marking as viewed in an after_action llamar de vuelta:

after_action :mark_as_seen

def index
  @unseen_activities = current_user.notifications.unseen.order(:updated_at => :desc)
  @seen_activities = current_user.notifications.seen.order(:updated_at => :desc)
end

private

def mark_as_seen
  current_user.notifications.update_all(:seen => true)
end

If that doesn't work, try to put an explicit render statement between fetching the notifications and updating there state.

If that doesn't work either, maybe mark them as seen in a background job, like Resque or Sidekiq.

Respondido el 12 de Septiembre de 13 a las 01:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.