# Submitting data to two different tables with two different m

**URL:** <https://www.ruby-forum.com/t/submitting-data-to-two-different-tables-with-two-different-m/102584>\
**Category:** Rails\
**Created:** [March 4, 2006, 4:00am UTC](https://www.ruby-forum.com/t/submitting-data-to-two-different-tables-with-two-different-m/102584 "2006-03-04T04:00:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 4, 2006, 4:00am UTC](https://www.ruby-forum.com/t/submitting-data-to-two-different-tables-with-two-different-m/102584/1 "2006-03-04T04:00:04Z")

</div>

I’m very new to Rails. I’ve been reading Agile Web Dev for a while and  
read  
up on ruby, but I’m still learning.

I designed a website for someone for an engineering shop. New projects  
had  
to be added manually.

Well, I figured this would be a great canidate for railifying! I have  
the  
form working perfectly, you can add your data and get it in a table, and  
listing projects works, too. I’m working on a photo gallery now, and I  
have  
a lot of it working. I took it from the agile web dev book. I’d like to  
know  
how to join the two forms and submit it as one.

I’m not sure how to submit data to two different places via one form.  
Suggestions and links are greatly appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 5, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/submitting-data-to-two-different-tables-with-two-different-m/102584/2 "2006-03-05T14:00:39Z")

</div>

Am Freitag, den 03.03.2006, 21:57 -0500 schrieb Matt R.:

> book. I’d like to know how to join the two forms and submit it as  
> one.
> 
> I’m not sure how to submit data to two different places via one form.  
> Suggestions and links are greatly appreciated. Thanks!

Take two text fields for example:

# view

\<%= form\_tag :controller =\> :posts, :action =\> create %\>  
\<%= text\_field(“post”, “title”, “size” =\> 20) %\>  
\<%= text\_field(“user”, “email”, “size” =\> 20) %\>  
\<%= end\_form\_tag %\>

# posts\_controller

class PostsController \< ApplicationController  
def create  
@post = Post.create(params[:post])  
@user = User.create(params[:user])  
@post.users \<\< @user  
end  
end

In the controller the params hash has one key for every object (user,  
post) in your form. As a values of this key you find a hash having a  
key-value pair for every object attribute (title, email).

## This way you can submit multiple objects via one form.

Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 5, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/submitting-data-to-two-different-tables-with-two-different-m/102584/3 "2006-03-05T14:06:41Z")

</div>

Norman - wouldn’t you also have to call a “@post.save” at the end to  
save the relationship?

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 8, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/submitting-data-to-two-different-tables-with-two-different-m/102584/4 "2006-03-08T22:09:23Z")

</div>

Thanks a lot, I got it working.

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 5, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/submitting-data-to-two-different-tables-with-two-different-m/102584/5 "2006-03-05T15:19:34Z")

</div>

Am Sonntag, den 05.03.2006, 07:05 -0600 schrieb Michael G.:

> > end  
> > end

It depends on the kind of association type binding the two models.

If User belongs\_to :post and Post has\_many :users, the \<\< method will  
call save on the child object (User) to make the association permanent.  
The same happens to child objects at has\_and\_belongs\_to\_many and has\_one  
associations. So @post.user = @user also needs no save on @user.

## In the difference (assume User belongs\_to :post as above), if you do @user.post = @post, you have to do a @user.save after associating them.

Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)
