Forum: Ruby on Rails problem: login Data save in two tables.

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
53daa996b4bf24f52b34d5145508ba88?d=identicon&s=25 (Guest)
on 2007-04-24 12:47
(Received via mailing list)
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 .

D7c511ce5025d37b8c6bd9134e0f2bd9?d=identicon&s=25 Thorsten (Guest)
on 2007-04-24 13:58
(Received via mailing list)
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 ;)

On 24 Apr., 12:46, ""
This topic is locked and can not be replied to.