# Relationship problems

**URL:** <https://www.ruby-forum.com/t/relationship-problems/190795>\
**Category:** Rails\
**Created:** [June 28, 2010, 11:22am UTC](https://www.ruby-forum.com/t/relationship-problems/190795 "2010-06-28T11:22:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![neilbain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b487fb/32.png) [@neilbain](https://www.ruby-forum.com/u/neilbain)\
**Post date:** [June 28, 2010, 11:22am UTC](https://www.ruby-forum.com/t/relationship-problems/190795/1 "2010-06-28T11:22:03Z")

</div>

I am trying to send an email using ActionMailer. I am having problems  
identifying the recipient in email.controller. I want it to be the owner  
of the current story.  
It won’t accept @story.user because I don’t know how to make a  
relationship between the story model and the email model. Trouble is  
user\_mailer won’t accept belongs\_to. Can anybody help?

---

<div class="post-metadata">

**Author:** ![neilbain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b487fb/32.png) [@neilbain](https://www.ruby-forum.com/u/neilbain)\
**Post date:** [June 28, 2010, 11:28am UTC](https://www.ruby-forum.com/t/relationship-problems/190795/2 "2010-06-28T11:28:06Z")

</div>

On 28 June 2010 10:22, Neil B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to send an email using ActionMailer. I am having problems  
> identifying the recipient in email.controller. I want it to be the owner  
> of the current story.  
> It won’t accept @story.user because I don’t know how to make a  
> relationship between the story model and the email model. Trouble is  
> user\_mailer won’t accept belongs\_to. Can anybody help?

:recipient =\> @story.user.email\_address

---

<div class="post-metadata">

**Author:** ![neilbain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b487fb/32.png) [@neilbain](https://www.ruby-forum.com/u/neilbain)\
**Post date:** [June 28, 2010, 12:37pm UTC](https://www.ruby-forum.com/t/relationship-problems/190795/3 "2010-06-28T12:37:20Z")

</div>

Michael P. wrote:

> :recipient =\> @story.user.email\_address

That gives undefined method `user’ for nil:NilClass

---

<div class="post-metadata">

**Author:** ![neilbain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b487fb/32.png) [@neilbain](https://www.ruby-forum.com/u/neilbain)\
**Post date:** [June 28, 2010, 2:58pm UTC](https://www.ruby-forum.com/t/relationship-problems/190795/4 "2010-06-28T14:58:57Z")

</div>

On 28 June 2010 11:37, Neil B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Michael P. wrote:
> 
> > :recipient =\> @story.user.email\_address
> 
> That gives undefined method `user’ for nil:NilClass

Well then the “It won’t accept @story.user because I don’t know how to  
make a relationship between the story model and the email model”  
would’ve returned that error too!

If you have a Story object somewhere, and the story instance has a  
user, and that user has an email address, you can pass it into your  
ActionMailer method as a parameter (or, as it looks like you’re doing,  
a hash value). You can then set that passed-in email address as the  
“recipients” for the email.

---

<div class="post-metadata">

**Author:** ![neilbain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b487fb/32.png) [@neilbain](https://www.ruby-forum.com/u/neilbain)\
**Post date:** [June 28, 2010, 6:35pm UTC](https://www.ruby-forum.com/t/relationship-problems/190795/5 "2010-06-28T18:35:06Z")

</div>

On 28 June 2010 15:18, Neil B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Problem is that it won’t recognise the Story object.

What Story object? In your controller code you don’t have one. You use  
a story key in the params hash, but you haven’t populated anything  
from the database. Is the email address in the params hash too?!  
Only you know where your data is.

> Doesn’t it need to be related?  
> To what? And what for?  
> You’re interesting in an email address of a person assigned to a  
> story. The UserMailer couldn’t care less about the relationships - it  
> just wants an email address. If you know the story’s ID, load it  
> (story = Story.find(story\_id\_from\_params\_or\_somewhere\_else)); then you  
> can call story.user.email\_address\_field\_name

---

<div class="post-metadata">

**Author:** ![neilbain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b487fb/32.png) [@neilbain](https://www.ruby-forum.com/u/neilbain)\
**Post date:** [June 28, 2010, 7:23pm UTC](https://www.ruby-forum.com/t/relationship-problems/190795/6 "2010-06-28T19:23:11Z")

</div>

Michael P. wrote:

> On 28 June 2010 15:18, Neil B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Problem is that it won’t recognise the Story object.
> 
> What Story object? In your controller code you don’t have one. You use  
> a story key in the params hash, but you haven’t populated anything  
> from the database. Is the email address in the params hash too?!  
> Only you know where your data is.
> 
> > Doesn’t it need to be related?  
> > To what? And what for?  
> > You’re interesting in an email address of a person assigned to a  
> > story. The UserMailer couldn’t care less about the relationships - it  
> > just wants an email address. If you know the story’s ID, load it  
> > (story = Story.find(story\_id\_from\_params\_or\_somewhere\_else)); then you  
> > can call story.user.email\_address\_field\_name

How do I tell it to find the current page?

---

<div class="post-metadata">

**Author:** ![neilbain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b487fb/32.png) [@neilbain](https://www.ruby-forum.com/u/neilbain)\
**Post date:** [June 28, 2010, 4:18pm UTC](https://www.ruby-forum.com/t/relationship-problems/190795/7 "2010-06-28T16:18:44Z")

</div>

Michael P. wrote:

> On 28 June 2010 11:37, Neil B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Michael P. wrote:
> > 
> > > :recipient =\> @story.user.email\_address
> > 
> > That gives undefined method `user’ for nil:NilClass

> If you have a Story object somewhere, and the story instance has a  
> user, and that user has an email address, you can pass it into your  
> ActionMailer method as a parameter (or, as it looks like you’re doing,  
> a hash value). You can then set that passed-in email address as the  
> “recipients” for the email.

How would I send it as a parameter then if it won’t accept my hash?.  
Problem is that it won’t recognise the Story object. Doesn’t it need to  
be related?
