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

Hi

   You can pass

class PartMailer < ActionMailer::Base   @admin_email = 'admin@sample.com'   @from_email = 'admin-sales@sample.com'

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 = 'admin@sample.com'   @from_email = 'admin-sales@sample.com'

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,

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:

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:

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

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