# Problem: login Data save in two tables

**URL:** <https://www.ruby-forum.com/t/problem-login-data-save-in-two-tables/92805>\
**Category:** Rails\
**Created:** [April 24, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/problem-login-data-save-in-two-tables/92805 "2007-04-24T12:47:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 24, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/problem-login-data-save-in-two-tables/92805/1 "2007-04-24T12:47:12Z")

</div>

Hi guys,

I have an user registration page. In that page there  
are 5 input fields.

I have to save user id and password in “login” table and other 3 input  
in “userinfo” table.

Regarding thid tropic I have two questions:

1. How can i retrive the post data from the input field?
2. How can i save data in two table by one model?

I use instants rails 1.4 and mysel 5

please give me some ideas? also waitng for some sample code .

Amin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 24, 2007, 1:58pm UTC](https://www.ruby-forum.com/t/problem-login-data-save-in-two-tables/92805/2 "2007-04-24T13:58:33Z")

</div>

You need 2 Models obviously, with an has\_one/belongs\_to association.

Class User \< ActiveRecord::Base  
has\_one :userinfo  
end

Class Userinfo \< ActiveRecord::Base  
table\_name “userinfo”  
belongs\_to  
end

the userinfo table has to have opne column “user\_id” for this  
association to work.

then you have 5 input fields in your view:

#login\_form.rhml

\<% form\_for :user, @user, :url =\> { :action =\> “login” } do |f| %\>  
\<%= f.text\_field “name” %\>  
\<%= f.password\_field “password” %\>

\<% fields\_for :userinfo, @user.userinfo do |u| %\>  
\<% u.text\_field1 :column1 %\>  
\<% u.text\_field2 :column2 %\>  
\<% u.text\_field3 :column3 %\>  
\<% end %\>

\<% end %\>

in your controller you can then access the data fo the 2 use fields  
with params[:user] (e.g. params[:user][:name]) and the userinfo with  
params[:userinfo] in the same way. you create an new user, and an  
associated userinfo item:

#controller  
…  
#Create the user (saves automatically)  
@user = User.create params[:user]  
#Create the associated Userinfo item (saves automatically)  
@user.create\_userinfo params[:userinfo]

…

If you find typos, keep them 😉

On 24 Apr., 12:46, “removed\_email\_address@domain.invalid”
