# Access from common class

**URL:** <https://www.ruby-forum.com/t/access-from-common-class/155863>\
**Category:** Rails\
**Created:** [December 17, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/access-from-common-class/155863 "2008-12-17T18:18:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [December 17, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/access-from-common-class/155863/1 "2008-12-17T18:18:46Z")

</div>

Hello!

I have made a common class and put it to the /lib folder of my Rails  
application.  
Now I have some problems with access to:

1. Mailers  
Call to SupportMailer.send\_activation\_email gives an error:  
NoMethodError (undefined method `send\_activation\_email’ for  
SupportMailer:Class):

Ok, I have changed my SupportMailer class - give a prefix to the  
function  
function send\_activation\_email -\> function self.send\_activation\_email  
But in this case the Mailer doesn’t see other models and gives an  
error:  
NoMethodError (You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.[]=):  
/app/models/support\_mailer.rb:14:in `send\_activation\_email’

How can I access mailers?

1. Functions from application.rb  
I have added a function (send\_sms) to the ApplicationController, but  
how can I access it from common class in /lib folder?  
I have tried:  
send\_sms  
ApplicationController::send\_sms  
But in both cases I have got NoMethodError

How can I access this global function?

I’m lost in all the dependencies and don’t know where to place global  
functions and classes and how to connect from them to another parts of  
the application…

---

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [December 17, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/access-from-common-class/155863/2 "2008-12-17T18:56:56Z")

</div>

On 17 Dec 2008, at 17:17, Konstantin wrote:

> SupportMailer:Class):

That’s not how mailers work. Typically your mailer method would be  
called activation\_email (and it must be an instance method - not a  
class method)  
Rails then magically lets you do  
SupportMailer.deliver\_activation\_email /  
SupportMailer.create\_activation\_email

> How can I access this global function?  
> It’s not a global function. It’s an instance method added to all  
> controllers
> 
> I’m lost in all the dependencies and don’t know where to place global  
> functions and classes and how to connect from them to another parts of  
> the application…

in foo.rb  
class Foo  
def self.some\_function  
end  
end

call Foo.some\_function anywhere you want (you could of course add the  
method to a more appropriate model class if there is one)

Fred

---

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [December 17, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/access-from-common-class/155863/3 "2008-12-17T22:44:13Z")

</div>

On Dec 17, 9:37 pm, Konstantin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> is an instance (not class) method…  
> What are you doing to try and send an email. It should be  
> SupportMailer.deliver\_activation\_email.

Fred

---

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [December 17, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/access-from-common-class/155863/4 "2008-12-17T22:38:21Z")

</div>

> It’s not a global function. It’s an instance method added to all  
> controllers

Ok, but I want to render a template in this function.  
How can I do that if I put it outside the controller (for example,  
into a separate class in a /lib folder)?

Other answers are really helpful, thank you 🙂  
Actually I still cannot force my Mailer to work, it reports about  
“undefined method `activation\_email’”, although this method exists and  
is an instance (not class) method…
