Thursday, July 15, 2010

[Rails] Re: ActiveRecord::Observer, update_all and has_many

I've worked out a solution. It might not be the neatest, but it works
for me:

has_many has its own call backs. So I can trigger a logging activity
using the 'after_remove' call back like this:

has_many :users, :after_remove => :log_removal

I am then able to grab the Observe object using ObjectSpace:

def get_an_active_activity_logger
active_loggers = Array.new
ObjectSpace.each_object(ActivityLogger){|l| active_loggers << l}
@activity_logger = active_loggers.first
end

And call one of @activity_logger's public methods to generate the log.

def log_removal(item)
get_an_active_activity_logger
@activity_logger.log_activity(
self,
'item_removal',
"#{item.class.name} #{item.id} removed from association"
)
end

It means I have to update each of my has_many calls and make the
log_removal and get_an_active_activity_logger methods available to the
models where they are needed, but both of those are fairly trivial
tasks.
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate