Nicholas Henry (Blend)
2005-Mar-28 04:23 UTC
Getting the class from an object passed to an observer give typeerror
I''m trying to get the class from an observer, for example: class MyObserver < ActiveRecord::Observer observe contact, organization def after_save(record) @record_class = record.class # some other processing end end Being new to Ruby and Rails I''m a little unsure why I get a TypeError of can''t dump anonymous class Class I have seen a discussion on this which leaves me a little blank ;) http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/128531 any ideas on how to get this functionality working in some capactity would be much appreciated. Thank you, Nicholas
Nicholas Henry
2005-Apr-04 16:47 UTC
Getting the class from an object passed to an observer give typeerror
I''m trying to get the class from an observer, for example: class MyObserver < ActiveRecord::Observer observe contact, organization def after_save(record) @record_class = record.class # some other processing end end Being new to Ruby and Rails I''m a little unsure why I get a TypeError of can''t dump anonymous class Class I have seen a discussion on this which leaves me a little blank ;) http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/128531 any ideas on how to get this functionality working in some capactity would be much appreciated. Thank you, Nicholas
Lucas Carlson
2005-Apr-04 17:16 UTC
Re: Getting the class from an object passed to an observer give typeerror
In what file have you defined your observer? It seems like the observer class doesn''t have access to the class definitions in the rest of your Rails app. -Lucas http://www.rufy.com/ On Apr 4, 2005, at 9:47 AM, Nicholas Henry wrote:> I''m trying to get the class from an observer, for example: > > class MyObserver < ActiveRecord::Observer > > observe contact, organization > > def after_save(record) > @record_class = record.class > # some other processing > end > > end > > Being new to Ruby and Rails I''m a little unsure why I get a TypeError > of > > can''t dump anonymous class Class > > I have seen a discussion on this which leaves me a little blank ;) > > http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/128531 > > any ideas on how to get this functionality working in some capactity > would be much appreciated. > > Thank you, > Nicholas > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Nicholas Henry
2005-Apr-05 22:21 UTC
Getting the class from an object passed to an observer give typeerror
I have followed the Hieraki example of creating a separate file for the observer class and defined the observer in the controller, for example: # file my_observer.rb class MyObserver < ActiveRecord::Observer observe contact def after_save(record) @record_class = record.class # some other processing end end --- # file contact_controller.rb class ContactController < ApplicationController model :Contact observer: my_observer # some more code end Also, I have done a breakpoint to inspect the local variable "record" in the observer and it does return what I would expect with the right class name. Also I did a record.methods which included all the methods I would expect, including "class". Cheers, Nicholas On Apr 4, 2005 1:16 PM, Lucas Carlson <rails-1eRuzFDw/cg@public.gmane.org> wrote:> In what file have you defined your observer? It seems like the observer > class doesn''t have access to the class definitions in the rest of your > Rails app. > > -Lucas > http://www.rufy.com/ > > On Apr 4, 2005, at 9:47 AM, Nicholas Henry wrote: > > > I''m trying to get the class from an observer, for example: > > > > class MyObserver < ActiveRecord::Observer > > > > observe contact, organization > > > > def after_save(record) > > @record_class = record.class > > # some other processing > > end > > > > end > > > > Being new to Ruby and Rails I''m a little unsure why I get a TypeError > > of > > > > can''t dump anonymous class Class > > > > I have seen a discussion on this which leaves me a little blank ;) > > > > http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/128531 > > > > any ideas on how to get this functionality working in some capactity > > would be much appreciated. > > > > Thank you, > > Nicholas > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Chris McGrath
2005-Apr-05 22:49 UTC
Re: Getting the class from an object passed to an observer give typeerror
The observer method only works in a controller. You can also do MyObserver.instance In config/environment.rb to make it work. Chris On Apr 5, 2005 11:21 PM, Nicholas Henry <nicholas.henry-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have followed the Hieraki example of creating a separate file for > the observer class and defined the observer in the controller, for > example: > > # file my_observer.rb > > class MyObserver < ActiveRecord::Observer > > observe contact > > def after_save(record) > @record_class = record.class > # some other processing > end > > end > > --- > > # file contact_controller.rb > > class ContactController < ApplicationController > > model :Contact > observer: my_observer > > # some more code > > end > > Also, I have done a breakpoint to inspect the local variable "record" > in the observer and it does return what I would expect with the right > class name. Also I did a record.methods which included all the methods > I would expect, including "class". > > Cheers, > Nicholas > > > On Apr 4, 2005 1:16 PM, Lucas Carlson <rails-1eRuzFDw/cg@public.gmane.org> wrote: > > In what file have you defined your observer? It seems like the observer > > class doesn''t have access to the class definitions in the rest of your > > Rails app. > > > > -Lucas > > http://www.rufy.com/ > > > > On Apr 4, 2005, at 9:47 AM, Nicholas Henry wrote: > > > > > I''m trying to get the class from an observer, for example: > > > > > > class MyObserver < ActiveRecord::Observer > > > > > > observe contact, organization > > > > > > def after_save(record) > > > @record_class = record.class > > > # some other processing > > > end > > > > > > end > > > > > > Being new to Ruby and Rails I''m a little unsure why I get a TypeError > > > of > > > > > > can''t dump anonymous class Class > > > > > > I have seen a discussion on this which leaves me a little blank ;) > > > > > > http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/128531 > > > > > > any ideas on how to get this functionality working in some capactity > > > would be much appreciated. > > > > > > Thank you, > > > Nicholas > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Nicholas Henry
2005-Apr-06 21:01 UTC
Re: Getting the class from an object passed to an observer give typeerror
As far as I know I do have it the controller. From my previous example: class ContactController < ApplicationController model :Contact observer: my_observer # some more code end On Apr 5, 2005 6:49 PM, Chris McGrath <c.r.mcgrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The observer method only works in a controller. You can also do > > MyObserver.instance > > In config/environment.rb to make it work. > > Chris > > On Apr 5, 2005 11:21 PM, Nicholas Henry <nicholas.henry-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I have followed the Hieraki example of creating a separate file for > > the observer class and defined the observer in the controller, for > > example: > > > > # file my_observer.rb > > > > class MyObserver < ActiveRecord::Observer > > > > observe contact > > > > def after_save(record) > > @record_class = record.class > > # some other processing > > end > > > > end > > > > --- > > > > # file contact_controller.rb > > > > class ContactController < ApplicationController > > > > model :Contact > > observer: my_observer > > > > # some more code > > > > end > > > > Also, I have done a breakpoint to inspect the local variable "record" > > in the observer and it does return what I would expect with the right > > class name. Also I did a record.methods which included all the methods > > I would expect, including "class". > > > > Cheers, > > Nicholas > > > > > > On Apr 4, 2005 1:16 PM, Lucas Carlson <rails-1eRuzFDw/cg@public.gmane.org> wrote: > > > In what file have you defined your observer? It seems like the observer > > > class doesn''t have access to the class definitions in the rest of your > > > Rails app. > > > > > > -Lucas > > > http://www.rufy.com/ > > > > > > On Apr 4, 2005, at 9:47 AM, Nicholas Henry wrote: > > > > > > > I''m trying to get the class from an observer, for example: > > > > > > > > class MyObserver < ActiveRecord::Observer > > > > > > > > observe contact, organization > > > > > > > > def after_save(record) > > > > @record_class = record.class > > > > # some other processing > > > > end > > > > > > > > end > > > > > > > > Being new to Ruby and Rails I''m a little unsure why I get a TypeError > > > > of > > > > > > > > can''t dump anonymous class Class > > > > > > > > I have seen a discussion on this which leaves me a little blank ;) > > > > > > > > http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/128531 > > > > > > > > any ideas on how to get this functionality working in some capactity > > > > would be much appreciated. > > > > > > > > Thank you, > > > > Nicholas > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >