# Access from common class

**URL:** <https://discuss.rubyonrails.org/t/access-from-common-class/37448>\
**Category:** rubyonrails-talk\
**Created:** [December 17, 2008, 5:17pm UTC](https://discuss.rubyonrails.org/t/access-from-common-class/37448 "2008-12-17T17:17:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Konstantin](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Konstantin](https://discuss.rubyonrails.org/u/Konstantin)\
**Post date:** [December 17, 2008, 5:17pm UTC](https://discuss.rubyonrails.org/t/access-from-common-class/37448/1 "2008-12-17T17:17:51Z")

</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: &nbsp;&nbsp;&nbsp;NoMethodError (You have a nil object when you didn't expect it! &nbsp;&nbsp;&nbsp;You might have expected an instance of ActiveRecord::Base. &nbsp;&nbsp;&nbsp;The error occurred while evaluating nil.=): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/app/models/support\_mailer.rb:14:in `send\_activation\_email'

How can I access mailers?

2. 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:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [December 17, 2008, 5:56pm UTC](https://discuss.rubyonrails.org/t/access-from-common-class/37448/2 "2008-12-17T17:56:30Z")

</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: &nbsp;&nbsp;NoMethodError (You have a nil object when you didn't expect it! &nbsp;&nbsp;You might have expected an instance of ActiveRecord::Base. &nbsp;&nbsp;The error occurred while evaluating nil.=): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/app/models/support\_mailer.rb:14:in `send\_activation\_email'

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 mailers?
> 
> 2. 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?

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 &nbsp;&nbsp;&nbsp;def self.some\_function &nbsp;&nbsp;&nbsp;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://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Konstantin](https://discuss.rubyonrails.org/u/Konstantin)\
**Post date:** [December 17, 2008, 9:37pm UTC](https://discuss.rubyonrails.org/t/access-from-common-class/37448/3 "2008-12-17T21:37:38Z")

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

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [December 17, 2008, 9:43pm UTC](https://discuss.rubyonrails.org/t/access-from-common-class/37448/4 "2008-12-17T21:43:23Z")

</div>

What are you doing to try and send an email. It should be SupportMailer.deliver\_activation\_email.

Fred
