Relationship problems

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?

Attachments: http://www.ruby-forum.com/attachment/4820/email_controller.rb

:recipient => @story.user.email_address

Michael Pavling 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.

Michael Pavling 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

Michael Pavling wrote: