# ActiveRecord Observers - can it only take one parameter?

**URL:** <https://www.ruby-forum.com/t/activerecord-observers-can-it-only-take-one-parameter/181866>\
**Category:** Rails\
**Created:** [January 19, 2010, 1:00pm UTC](https://www.ruby-forum.com/t/activerecord-observers-can-it-only-take-one-parameter/181866 "2010-01-19T13:00:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [January 19, 2010, 1:00pm UTC](https://www.ruby-forum.com/t/activerecord-observers-can-it-only-take-one-parameter/181866/1 "2010-01-19T13:00:52Z")

</div>

hi, guys,

I refered to [Action Mailer Basics — Ruby on Rails Guides](http://guides.rubyonrails.org/action_mailer_basics.html)  
and [Ruby on Rails API](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 = ‘removed\_email\_address@domain.invalid’  
@from\_email = ‘removed\_email\_address@domain.invalid’

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

> **[Action Mailer Basics — Ruby on Rails Guides](https://guides.rubyonrails.org/action_mailer_basics.html)**
>
> Action Mailer BasicsThis guide provides you with all you need to get started in sending emails from your application, and many internals of Action Mailer. It also covers how to test your mailers.After reading this guide, you will know: How to send...

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

Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [January 21, 2010, 3:13pm UTC](https://www.ruby-forum.com/t/activerecord-observers-can-it-only-take-one-parameter/181866/2 "2010-01-21T15:13:27Z")

</div>

Hi

You can pass

> class PartMailer \< ActionMailer::Base  
> @admin\_email = ‘removed\_email\_address@domain.invalid’  
> @from\_email = ‘removed\_email\_address@domain.invalid’

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

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [January 21, 2010, 6:38pm UTC](https://www.ruby-forum.com/t/activerecord-observers-can-it-only-take-one-parameter/181866/3 "2010-01-21T18:38:30Z")

</div>

Sijo k g wrote:

> Hi
> 
> You can pass
> 
> > class PartMailer \< ActionMailer::Base  
> > @admin\_email = ‘removed\_email\_address@domain.invalid’  
> > @from\_email = ‘removed\_email\_address@domain.invalid’
> 
> 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [January 21, 2010, 11:57pm UTC](https://www.ruby-forum.com/t/activerecord-observers-can-it-only-take-one-parameter/181866/4 "2010-01-21T23:57:20Z")

</div>

On Thu, Jan 21, 2010 at 1:01 PM, Gordon Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [January 21, 2010, 10:03pm UTC](https://www.ruby-forum.com/t/activerecord-observers-can-it-only-take-one-parameter/181866/5 "2010-01-21T22:03:46Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [January 22, 2010, 12:17am UTC](https://www.ruby-forum.com/t/activerecord-observers-can-it-only-take-one-parameter/181866/6 "2010-01-22T00:17:15Z")

</div>

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