Rails 3 - Obtaining the Lates Observer Record
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 audi开发者_开发百科t 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
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
精彩评论