# Creating 2 things at the same time?

**URL:** <https://www.ruby-forum.com/t/creating-2-things-at-the-same-time/176660>\
**Category:** Rails\
**Created:** [October 4, 2009, 10:34am UTC](https://www.ruby-forum.com/t/creating-2-things-at-the-same-time/176660 "2009-10-04T10:34:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [October 4, 2009, 10:34am UTC](https://www.ruby-forum.com/t/creating-2-things-at-the-same-time/176660/1 "2009-10-04T10:34:09Z")

</div>

So I have 2 models, Comment and Picture. A Comment has one Picture if  
the users so chooses, but when someone is creating a Comment, it would  
of course call the create method in the Comment Controller. However, I  
want it to create a Picture as well. I could add it to the create  
method in the Comment Controller, but that does not seem very DRY. Is  
there a way I can do this?

---

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [October 5, 2009, 6:52am UTC](https://www.ruby-forum.com/t/creating-2-things-at-the-same-time/176660/2 "2009-10-05T06:52:10Z")

</div>

Question is not clear, do you want to create an empty picture record  
anyway?

---

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [October 5, 2009, 8:50am UTC](https://www.ruby-forum.com/t/creating-2-things-at-the-same-time/176660/3 "2009-10-05T08:50:17Z")

</div>

Hi Mike Chai

What I understood is you have relations like

comment has\_one picture  
picture belongs\_to comment

As an example  
comment name:string  
picture name:string,comment\_id:integer

So do as below

in comments\_controller new action  
@comment = Comment.new  
@comment.picture = Picture.new

```
Now in view/comments/new.html.erb

```

\<% form\_for(@comment) do |f| %\>

\<%= f.label :name %\>  
 \<%= f.text\_field :name %\>

\<% f.fields\_for :picture do |p| %\> \<%= p.text\_field :name %\> \<% end %\>

\<%= f.submit 'Create' %\>

\<% end %\>

```
 in model Comment write (modify this according to your needs)

```

has\_one :picture,:dependent =\> :destroy  
accepts\_nested\_attributes\_for :picture,:reject\_if =\> proc { |attributes|  
attributes[‘name’].blank? }

```
 in create action of comments controller

```

@comment = Comment.new(params[:comment])  
@comment.save

Thats all…Modify the rest of your code like this…This is just an  
example

Sijo

---

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [October 5, 2009, 8:52am UTC](https://www.ruby-forum.com/t/creating-2-things-at-the-same-time/176660/4 "2009-10-05T08:52:34Z")

</div>

And assuming you are on rails 2.3
