views:

48

answers:

1

Here's the current flow in my Rails App 1. User uses AJAX and creates a Photo record (stored in DB) 2. Then, the observer catches this and adds a record to the audit log

** That was all without a page refresh. The challenge I have now is that I need to have AJAX return a "news feed" item from the AuditLog created by the observer. It needs to be from the auditlog as I need the auditlog.id for that record.

Ideas? thanks

+1  A: 

Assuming your models are as follows:

class AuditLog < ActiveRecord::Base
  belongs_to :auditable, :polymorphic => true
end

class Photo < ActiveRecord::Base
  has_many :audit_logs, :as => :auditable
end

The observer callbacks are like regular before and after filters on a model. The callback happens in the same execution block as the save call. So you should be able to access the AuditLog objects using the association on the Photo model.

if photo.save
  audit_log = photo.audit_logs(:order => "id DESC").first
  # return the audit_log feed
else
  # error
end
KandadaBoggu