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