hi, guys, I refered to http://guides.rubyonrails.org/action_mailer_basics.html and http://api.rubyonrails.org// I am a bit unclear about observers. Consider the set up below: model/part_mailer.rb ---------------------------- class PartMailer < ActionMailer::Base @admin_email = ''admin-p4yIsiyCo27QT0dZR+AlfA@public.gmane.org'' @from_email = ''admin-sales-p4yIsiyCo27QT0dZR+AlfA@public.gmane.org'' def created_succesfully(part) recipients user.email, @admin_email from @from_email subject "MyApp - New part created." body :user => user end model/part_observer.rb ------------------------------- class PartObserver < ActiveRecord::Observer def after_create(part) PartMailer.deliver_created_succesfully(part) end end It looks as if I can only pass in the object of the given class into the observer. In the case above, ''part'' is the only one that is being passed by to deliver_created_successfully in model/part_observer.rb. Can I pass more objects to the method, created_successfully (model/ part_mailer.rb)? I tried looking at the api docs and http://guides.rubyonrails.org/action_mailer_basics.html to no success. Can someone please shed some light into this? Thanks! :) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sijo k g
2010-Jan-21 14:13 UTC
Re: ActiveRecord Observers - can it only take one parameter?
Hi You can pass> class PartMailer < ActionMailer::Base > @admin_email = ''admin-p4yIsiyCo27QT0dZR+AlfA@public.gmane.org'' > @from_email = ''admin-sales-p4yIsiyCo27QT0dZR+AlfA@public.gmane.org''These are instance variables.If need this entire for the class declare it as class varibale using @@>And here do like (if this is what you need) def created_succesfully(part,pass_what_you_need_here)> recipients user.email, @admin_email > from @from_email > subject "MyApp - New part created." > body :user => userend>Do you mean like this ?> class PartObserver < ActiveRecord::Observerdef after_create(part,obj_or_var) PartMailer.deliver_created_succesfully(part) end> end > >Sijo -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jan-21 17:38 UTC
Re: ActiveRecord Observers - can it only take one parameter?
Sijo k g wrote:> Hi > > You can pass >> class PartMailer < ActionMailer::Base >> @admin_email = ''admin-p4yIsiyCo27QT0dZR+AlfA@public.gmane.org'' >> @from_email = ''admin-sales-p4yIsiyCo27QT0dZR+AlfA@public.gmane.org'' > > These are instance variables.If need this entire for the class declare > it as class varibale using @@No, actually, that part is fine. Since the @variables are defined outside of a method, they are instance variables on the Class object -- they function like @@variables but are apparently more reliable. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gordon Yeong
2010-Jan-21 21:01 UTC
Re: Re: ActiveRecord Observers - can it only take one parameter?
hi sijo, morning. Thanks for that. 1. Yes I did use class variables in the end. 2. For the parameter arguments to the observer and mailer, yes I did pass in 2 arguments being "user, part" and I got an error being "2 for 1 error" or that the object, "user" was nil (which is weird because I set up print statements in the observer to print out various attributes of the "user" object (ie. first name, surname and so forth) and it worked fine. For the time being I stuck to passing 1 parameter argument and used the object''s associations to grab the other object when I needed it down the line. It''s been working well. Has any body actually done this before ( as in pass in more than 1 parameter to the observer and the corresponding action method (in my case the deliver_created_successfully method)? Thank you :) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Conrad Taylor
2010-Jan-21 22:55 UTC
Re: Re: ActiveRecord Observers - can it only take one parameter?
On Thu, Jan 21, 2010 at 1:01 PM, Gordon Yeong <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> hi sijo, > morning. Thanks for that. > > 1. Yes I did use class variables in the end. > 2. For the parameter arguments to the observer and mailer, yes I did pass > in 2 arguments being "user, part" and I got an error being "2 for 1 error" > or that the object, "user" was nil (which is weird because I set up print > statements in the observer to print out various attributes of the "user" > object (ie. first name, surname and so forth) and it worked fine. > > For the time being I stuck to passing 1 parameter argument and used the > object''s associations to grab the other object when I needed it down the > line. It''s been working well. > > Has any body actually done this before ( as in pass in more than 1 > parameter to the observer and the corresponding action method (in my case > the deliver_created_successfully method)? > > > > Thank you :) > > >Yes, you can use @@some_variable outside of a instance method definition and access it within an instance method as follows: @@some_variable Now, if you use @some_variable outside of a instance method definition, you can do the following to access it: @some_variable = some_value_0 class << self attribute_accessor :some_variable end Then you''ll do the following within an instance method: PartMailer.some_variable = some_value_1 puts PartMailer.some_variable In short, top level instance variables loose their scope and must be qualified using the class name (i.e. <ClassName>.variable_name) within instance methods instead of using @variable_name. However, you have direct access to a top level instance variable within a class method. For example, take the following: class SomeVariable @some_variable = 1 class << self attr_accessor :some_variable end def inner print "inner method without qualification => " p @some_variable print "inner method with qualification => " p SomeVariable.some_variable end def self.outer print "outer method => " p @some_variable end end var = SomeVariable.new var.inner SomeVariable.outer Good luck, -Conrad> > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gordon Yeong
2010-Jan-21 23:14 UTC
Re: Re: ActiveRecord Observers - can it only take one parameter?
thanks, conrad, makes sense. I will test it out. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.