ActiveRecord Observers - can it only take one parameter?

hi, guys,

I refered to Action Mailer Basics — Ruby on Rails Guides
and Ruby on Rails API

I am a bit unclear about observers.

Consider the set up below:

model/part_mailer.rb

class PartMailer < ActionMailer::Base
@admin_email = ‘[email protected]
@from_email = ‘[email protected]

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

to
no success. Can someone please shed some light into this?

Thanks! :slight_smile:

Hi

You can pass

class PartMailer < ActionMailer::Base
@admin_email = ‘[email protected]
@from_email = ‘[email protected]

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 => user

end

Do you mean like this ?

class PartObserver < ActiveRecord::Observer
def after_create(part,obj_or_var)
PartMailer.deliver_created_succesfully(part)
end
end

Sijo

Sijo k g wrote:

Hi

You can pass

class PartMailer < ActionMailer::Base
@admin_email = ‘[email protected]
@from_email = ‘[email protected]

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
[email protected]

On Thu, Jan 21, 2010 at 1:01 PM, Gordon Y. [email protected]
wrote:

For the time being I stuck to passing 1 parameter argument and used the

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. .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

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 :slight_smile:

thanks, conrad, makes sense. I will test it out.