Why use an Observer for a single model rather than using a callback in the ActiveRecord object? They seem like they work identically. Is there a difference I''m missing? For example, these seem like they would do the same thing: class User < ActiveRecord::Base after_create :send_welcome protected def send_welcome UserMailer.send_welcome(self) end end class UserObserver < ActiveRecord::Observer def after_create(user) UserMailer.send_welcome(user) end end -- TW --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi tekwiz, This is excerpt is from Agile Web Development with Rails: Callbacks are a fine technique, but they can sometimes result in a model class taking on responsibilities that aren’t really related to the nature of the model. For example, on page 385 we created a callback that generated a log message when an order was created. That functionality isn’t really part of the basic Order class—we put it there because that’s where the callback executed. Active Record observers overcome that limitation. An observer transparently links itself into a model class, registering itself for callbacks as if it were part of the model but without requiring any changes in the model itself. On Mar 20, 4:11 pm, tekwiz <twarl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Why use an Observer for a single model rather than using a callback in > the ActiveRecord object? They seem like they work identically. Is > there a difference I''m missing? > > For example, these seem like they would do the same thing: > > class User < ActiveRecord::Base > after_create :send_welcome > > protected > def send_welcome > UserMailer.send_welcome(self) > end > end > > class UserObserver < ActiveRecord::Observer > def after_create(user) > UserMailer.send_welcome(user) > end > end > > -- TW--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---
Thanks elioncho! I understand that it is a best practice to separate certain functionality out of models into observers. I guess I should clarify my question... Is there any _technical_ reason to use observers instead of callbacks or visa versa. -- TW On Mar 20, 8:15 pm, elioncho <elion...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi tekwiz, > > This is excerpt is from Agile Web Development with Rails: > > Callbacks are a fine technique, but they can sometimes result in a > model class > taking on responsibilities that aren’t really related to the nature of > the model. > For example, on page 385 we created a callback that generated a log > message > when an order was created. That functionality isn’t really part of the > basic > Order class—we put it there because that’s where the callback > executed. > Active Record observers overcome that limitation. An observer > transparently > links itself into a model class, registering itself for callbacks as > if it were part > of the model but without requiring any changes in the model itself. > > On Mar 20, 4:11 pm, tekwiz <twarl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Why use an Observer for a single model rather than using a callback in > > the ActiveRecord object? They seem like they work identically. Is > > there a difference I''m missing? > > > For example, these seem like they would do the same thing: > > > class User < ActiveRecord::Base > > after_create :send_welcome > > > protected > > def send_welcome > > UserMailer.send_welcome(self) > > end > > end > > > class UserObserver < ActiveRecord::Observer > > def after_create(user) > > UserMailer.send_welcome(user) > > end > > end > > > -- TW--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---
I''d really like to know the answer to that too! On Mar 21, 11:52 am, tekwiz <twarl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks elioncho! I understand that it is a best practice to separate > certain functionality out of models into observers. > > I guess I should clarify my question... Is there any _technical_ > reason to use observers instead of callbacks or visa versa. > > -- TW > > On Mar 20, 8:15 pm, elioncho <elion...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi tekwiz, > > > This is excerpt is from Agile Web Development with Rails: > > > Callbacks are a fine technique, but they can sometimes result in a > > model class > > taking on responsibilities that aren’t really related to the nature of > > the model. > > For example, on page 385 we created a callback that generated a log > > message > > when an order was created. That functionality isn’t really part of the > > basic > > Order class—we put it there because that’s where the callback > > executed. > > Active Record observers overcome that limitation. An observer > > transparently > > links itself into a model class, registering itself for callbacks as > > if it were part > > of the model but without requiring any changes in the model itself. > > > On Mar 20, 4:11 pm, tekwiz <twarl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Why use an Observer for a single model rather than using a callback in > > > the ActiveRecord object? They seem like they work identically. Is > > > there a difference I''m missing? > > > > For example, these seem like they would do the same thing: > > > > class User < ActiveRecord::Base > > > after_create :send_welcome > > > > protected > > > def send_welcome > > > UserMailer.send_welcome(self) > > > end > > > end > > > > class UserObserver < ActiveRecord::Observer > > > def after_create(user) > > > UserMailer.send_welcome(user) > > > end > > > end > > > > -- TW