Problem: login Data save in two tables

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 .


You need 2 Models obviously, with an has_one/belongs_to association.

Class User < ActiveRecord::Base
has_one :userinfo

Class Userinfo < ActiveRecord::Base
table_name “userinfo”

the userinfo table has to have opne column “user_id” for this
association to work.

then you have 5 input fields in your view:


<% 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:


#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 :wink:

On 24 Apr., 12:46, “[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs