# Using an ActiveRecord class method within an ActionMailer object

**URL:** <https://www.ruby-forum.com/t/using-an-activerecord-class-method-within-an-actionmailer-object/150444>\
**Category:** Rails\
**Created:** [October 8, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/using-an-activerecord-class-method-within-an-actionmailer-object/150444 "2008-10-08T17:35:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [October 8, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/using-an-activerecord-class-method-within-an-actionmailer-object/150444/1 "2008-10-08T17:35:29Z")

</div>

This is a little convoluted and I’m new to Rails, so please bear with  
me…

My application needs to receive both SMS and emails. I have a  
controller that has a sms\_in method, which receives SMS messages via a  
HTTP POST request from an SMS gateway. That controller then calls the  
IncomingMessage.process(args) method to go through a number of  
validations and create the incoming message in the database as well as  
various other database entries that are required with each incoming  
message. This works fine.

However, I’m having trouble getting it to work for emails. In my  
MailProcessor (ActionMailer) receive method, I’m trying to pass  
similar arguments to the IncomingMessage.process(args) class method.  
However, for some reason this isn’t working…I can’t seem to call a  
class method for an ActiveRecord object from within the ActionMailer  
object, and I have no idea why!

Any help would be greatly appreciated!  
-Gavin

---

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [October 8, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/using-an-activerecord-class-method-within-an-actionmailer-object/150444/2 "2008-10-08T17:52:30Z")

</div>

On 8 Oct 2008, at 16:34, gaveeno wrote:

> message. This works fine.
> 
> However, I’m having trouble getting it to work for emails. In my  
> MailProcessor (ActionMailer) receive method, I’m trying to pass  
> similar arguments to the IncomingMessage.process(args) class method.  
> However, for some reason this isn’t working…I can’t seem to call a  
> class method for an ActiveRecord object from within the ActionMailer  
> object, and I have no idea why!

That should work. Show what you’re doing (and describe what you  
actually mean by “isn’t working”)

Fred

---

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [October 8, 2008, 6:37pm UTC](https://www.ruby-forum.com/t/using-an-activerecord-class-method-within-an-actionmailer-object/150444/3 "2008-10-08T18:37:05Z")

</div>

I’m using the Fetcher plugin which daemonizes polling the server for  
emails. When it pulls in an email, it passes it (the Net::Imap  
object) to the receive method of MailProcessor \< ActionMailer::Base.  
The receive method then calls the process\_message class method of  
IncomingMessage \< ActiveRecord::Base, which should store the incoming  
message to the database as well as do a number of other things.  
However, this method does not seem to be getting executed for incoming  
emails. On the other hand, when this method is called from the  
controller containing the sms\_in action, the message gets processed/  
stored correctly. These are the methods in question:

class MailProcessor \< ActionMailer::Base  
def receive(mail)  
IncomingMessage.process\_message({ :sender =\> mail.from,  
:subject =\> mail.subject,  
:channel =\> “email”,  
:person\_search\_field =\> “email”,  
:date =\> mail.date,  
:body =\> mail.body,  
:delivery\_point =\> mail.to })  
end

class MessengerController \< ApplicationController  
def sms\_in  
@incoming\_message, @opt\_or\_feedback\_object,@processing\_notes =  
IncomingMessage.process\_message({ :sender =\> params[:smsfrom],  
:subject =\> params[:smsmsg],  
:channel =\> “sms”,  
:gateway\_id =\> params[:smsid],  
:person\_search\_field =\> “mobile\_phone”,  
:date =\> params[:smsdate],  
:carrier =\> params[:network],  
:delivery\_point =\> params[:smsto] })  
end

class IncomingMessage \< ActiveRecord::Base  
protected  
def self.process\_message(msg\_hash)  
@incoming\_message = create\_incoming\_message  
#more code to process the message…I didn’t paste it here  
because it calls  
#a number of other class methods and they would be too much to  
paste  
end

def self.create\_incoming\_message  
IncomingMessage.create(:subject =\> @msg\_hash[:subject],  
:person\_id =\> “#{@person.id}”,  
:body =\> @msg\_hash[:body],  
:channel =\> @msg\_hash[:channel],  
:gateway\_id =\> @msg\_hash[:gateway\_id],  
:carrier =\> @msg\_hash[:carrier],  
:date =\> @msg\_hash[:date],  
:delivery\_point\_id =\> @delivery\_point\_id)  
end

On Oct 8, 11:51 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [October 10, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/using-an-activerecord-class-method-within-an-actionmailer-object/150444/4 "2008-10-10T18:42:06Z")

</div>

On Oct 10, 5:32 pm, gaveeno [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Anybody know why this isn’t working for me?

Well you haven’t said exactly what is happening, eg if you put a  
breakpoint in create\_incoming\_message is it hit? have you tried  
stepping through the code and seeing when it fails?  
Could it be that creation of the incoming\_message fails because of  
validation? Do you really mean to be creating class instance  
variables ?

Fred

---

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [October 10, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/using-an-activerecord-class-method-within-an-actionmailer-object/150444/5 "2008-10-10T18:32:42Z")

</div>

Anybody know why this isn’t working for me?

---

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [October 15, 2008, 8:00am UTC](https://www.ruby-forum.com/t/using-an-activerecord-class-method-within-an-actionmailer-object/150444/6 "2008-10-15T08:00:43Z")

</div>

What SMS gateway are you using if you don’t mind me asking? Is it  
open source or are you paying per message?

---

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [October 15, 2008, 8:03pm UTC](https://www.ruby-forum.com/t/using-an-activerecord-class-method-within-an-actionmailer-object/150444/7 "2008-10-15T20:03:49Z")

</div>

We are using a shared shortcode provided by another company, so nope  
it’s not open source. As far as I’m aware, open source SMS gateways  
such as Kannel still require that you have a shortcode leased and  
registered with the various carriers, which is really expensive.  
There’s really doesn’t seem to be any way to avoid a per-message cost  
for outgoing messages (many mobile gateway providers don’t charge for  
incoming messages).

---

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [October 16, 2008, 2:41am UTC](https://www.ruby-forum.com/t/using-an-activerecord-class-method-within-an-actionmailer-object/150444/8 "2008-10-16T02:41:31Z")

</div>

I have installed and am using SMS-Fu:  
[http://brendanlim.com/2008/2/28/send-text-messages-from-rails-with-sms-fu](http://brendanlim.com/2008/2/28/send-text-messages-from-rails-with-sms-fu),  
which appears to be free to send sms messages. Maybe there is a limit  
with this, I am not sure, I read somewhere 500 max in any given 3 day  
period but maybe it varies for different carriers? What I would  
really like is to be able to receive SMS for free, but it doesnt look  
like its in the cards.

---

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [October 10, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/using-an-activerecord-class-method-within-an-actionmailer-object/150444/9 "2008-10-10T19:19:38Z")

</div>

When you ask if I really mean to be creating class instance variables,  
I assume you’re referring to @incoming\_message,  
@opt\_or\_feedback\_object, @processing\_notes =  
IncomingMessage.process\_message()…correct? The reason I set class  
instance variables is because the view for this action needs to  
display details from the incoming message, the object created from the  
the incoming message, and the notes that are generated during  
processing (including the validation results mostly). I didn’t paste  
the last line of the IncomingMessage.process\_message() method above,  
but FYI it is “return  
@incoming\_message,@opt\_or\_feedback\_object,@processed\_msg\_array[0]”.

No, the reason is not that it’s failing because of validations within  
the IncomingMessage.process\_message() method, because the terminal  
window shows now SQL being executed, and also there are items  
persisted to the DB regardless of whether validation succeeds or  
fails. I took your tip and used breakpoints to see what’s happening  
here (this is my first Rails app and I stupidly didn’t think to look  
into the Rails debug tools). When a SMS comes in, the breakpoint at  
the start of the IncomingMessage.process\_message() method is hit.  
However, when an email comes in, not breakpoints are hit, regardless  
of where they are. I.e. in the MailProcesser.receive() method, I put  
the following 2 lines:  
1 puts “debug”  
2 debugger  
The terminal window where I executed the script displays “debug”, but  
processing does not stop at the breakpoint.

This might help…it’s the fetcher script that is daemonized (and I  
can post more of the code from the Fetcher plugin if you think that  
it’d be helpful):

#!/usr/bin/env ruby  
require File.dirname( **FILE** ) + ‘/…/config/environment.rb’

class MailFetcherDaemon \< Daemon::Base

@config = YAML.load\_file(“#{RAILS\_ROOT}/config/mail.yml”)  
@config = @config[RAILS\_ENV].to\_options

@sleep\_time = @config.delete(:sleep\_time) || 60  
def self.start  
puts “Starting MailFetcherDaemon”  
@fetcher = Fetcher.create({:receiver =\>  
MailProcessor}.merge(@config))

```
loop do
  @fetcher.fetch
  sleep(@sleep_time)
end

```

end

def self.stop  
puts “Stopping MailFetcherDaemon”  
end

end

MailFetcherDaemon.daemonize

Thanks for your help!  
-Gavin

On Oct 10, 12:41 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
