q: observer

12 views
Skip to first unread message

tom

unread,
Nov 19, 2015, 9:06:57 AM11/19/15
to Ruby on Rails: Talk
hi,

is this the right thing todo?:


class AuditorObserver < ActiveRecord::Observer

  observe :order, :feedback,:jobapplicant

   def after_create(record)
if record.class.name=="Order"
             ............
        end
if record.class.name=="Feedback"
             ............
        end
        ............



thx



Juan Pablo Avello

unread,
Nov 20, 2015, 4:01:34 AM11/20/15
to Ruby on Rails: Talk
- If you need to do different things to each class you probably want different observers for each one. Anyway, if they all share the attributes needed inside the observer you don't really need those "if".
- A better looking alternative to 'record.class.name=="Order"' could be: 'record.is_a?(Order)' ; a lot shorter and looks nicer.
- You could also add some private/protected methods instead of placing code inside multiple "if" statements. Something like "process_orders(record) if record.is_a?(Order)"
Reply all
Reply to author
Forward
0 new messages