# Passing an instance of one object to another

**URL:** <https://www.ruby-forum.com/t/passing-an-instance-of-one-object-to-another/115037>\
**Category:** Rails\
**Created:** [September 20, 2007, 12:27am UTC](https://www.ruby-forum.com/t/passing-an-instance-of-one-object-to-another/115037 "2007-09-20T00:27:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![skoppy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@skoppy](https://www.ruby-forum.com/u/skoppy)\
**Post date:** [September 20, 2007, 12:27am UTC](https://www.ruby-forum.com/t/passing-an-instance-of-one-object-to-another/115037/1 "2007-09-20T00:27:37Z")

</div>

Hello,

I’m working on my first Rails app. It’s a redesign for a magazine  
site. I have a class for the articles and a class for issues. I’ve  
set up a has\_many/belongs\_to relationship between the two so that  
every article belongs to a particular issue.

Now I’d like to set it up so that a new article cannot be added UNLESS  
you have already chosen the issue that it is to be added to. I’d like  
to set something up like  
“selected\_issue.articles.build(params[:article])” but I don’t know how  
to create an action/method that would allow me to set the  
selected\_issue variable and then pass that into the create method on  
the article controller.

Could someone lend some assistance here?

Thanks.

---

<div class="post-metadata">

**Author:** ![skoppy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@skoppy](https://www.ruby-forum.com/u/skoppy)\
**Post date:** [September 20, 2007, 5:44am UTC](https://www.ruby-forum.com/t/passing-an-instance-of-one-object-to-another/115037/2 "2007-09-20T05:44:05Z")

</div>

you got it right. you can call  
@issue.articles.create( params[:article] ) to get a new article that  
is already saved and associated with the issue. you can also do  
@issue.articles.build( params[:article] ) if you want to get a new  
article object but not save it yet.

see here:  
[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000642](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000642)

---

<div class="post-metadata">

**Author:** ![skoppy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@skoppy](https://www.ruby-forum.com/u/skoppy)\
**Post date:** [September 20, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/passing-an-instance-of-one-object-to-another/115037/3 "2007-09-20T19:44:13Z")

</div>

On 9/19/07, skoppy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> to set something up like  
> “selected\_issue.articles.build(params[:article])” but I don’t know how  
> to create an action/method that would allow me to set the  
> selected\_issue variable and then pass that into the create method on  
> the article controller.

A common way to do this is for your “create article” form to have a  
select field for issue\_id that allows the user to select from  
available issues.

In your view:

\<% form\_for :article do |f| %\>  
\<% select :issue\_id, Issue.find(:all).collect {|p| [ p.name, p.id  
] } %\>  
…  
\<% end %\>

Then in your controller,

@article = new Article(params[:article])  
if @article.save  
…blah blah

This will automatically assign the article to the selected issue.

If you want to ensure that the article is linked to a valid issue, add  
to your model:

class Article \< ActiveRecord::Base  
belongs\_to :issue  
validates\_presence\_of :issue  
end
