Retrieve data from two Tables and validate

Hi ALL,

I have created two tables in mysql to store user details.

CREATE TABLE IF NOT EXISTS users (
id int(11) NOT NULL auto_increment,
user_name CHAR( 20 ) default NULL,
password varchar(255) default NULL,
recovery_question varchar(255) default NULL,
recovery_answer varchar(255) default NULL,
user_type varchar(255) NOT NULL,
status tinyint(1) default NULL,
email varchar(255) NOT NULL,
create_date TIMESTAMP NOT NULL,
update_date TIMESTAMP NOT NULL,
PRIMARY KEY (id),
UNIQUE INDEX ( user_name,user_type )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
AUTO_INCREMENT=5 ;# MySQL returned an empty result set (i.e. zero rows).

CREATE TABLE IF NOT EXISTS personal_details (
id int(11) NOT NULL auto_increment,
user_id varchar(255) NOT NULL,
title varchar(5) default NULL,
first_name varchar(20) default NULL,
last_name varchar(20) default NULL,
current_location varchar(255) default NULL,
phone_number int(10) default NULL,
mobile_number int(10) default NULL,
national_id_or_passport_no varchar(255) default NULL,
create_date TIMESTAMP NOT NULL,
update_date TIMESTAMP NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
AUTO_INCREMENT=1;# MySQL returned an empty result set (i.e. zero rows).

in may application when the user enter the login details he will be
validated and directed to this home page where his personal data will
from the personal_details will be displayed.

to retrieve data from the user table i used the following command
<% @user = User.find_first(["Id = "+@u_id]) %>

when it come to retrieve data from the personal_details table i used the
same command like
<% @user = EmployerRepresentativeDetail.find_first(["user_id =
"+@u_id]) %>

so i want to know instead of using like this is their a way to get the
whole data’s in different tables that belongs to a one use.

Also another prob i have is have a give a option to edit his personal
details. where i have taken all the fields in the above two tables in to
one page and allow to edit the and save it. at this point i faces
problem like how to validate and save the things that belong to two
different table and where the validation happen in two different models.

so plssss need some advice from u All…

:slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Instead of using:

<% @user = User.find_first(["Id = "+@u_id]) %>

Put that code in the controller and not the view, and use:

@user = User.find(@u_id)

find_first and find_all are being deprecated in Rails 2.0, so it’s best
to
get out of the habit of using them before Rails 2.0 becomes mainstream.

Ryan wrote:

Instead of using:

<% @user = User.find_first(["Id = "+@u_id]) %>

Put that code in the controller and not the view, and use:

@user = User.find(@u_id)

find_first and find_all are being deprecated in Rails 2.0, so it’s best
to
get out of the habit of using them before Rails 2.0 becomes mainstream.

ok thanks for the tip.

by the way i used those commands in the controller not in the view i
used <% %> to show the codes only. sorry for that mistake.

bt PAL still i have not found the way to solve the problem i faced.

  1. the way to retrieve many recodes from many tables that belongs to the
    same person, is the way i used or in ROR is their a efficient way to do
    it.

  2. the problem mention in the updating two table thing.

Thnaks

I’m in a similar situation.

I have a user table :users with crucial login info, and another one
for profile data :members. :users actually belongs to a plug
in; :members is mine. I could put all the info into :users I suppose,
but what I really want to do is to keep them separate but still enjoy
the benefits of Model and all the form handlng and CRUD goodness. I
thought maybe

class Member < User

end

would do it, and it does, provided I put all the data in :users and
Member just allows me to not mess with the plug-in’s source.

Can a model be associated with 2 tables?

I’m not sure I’m understanding you correctly, but you may be able to
do this:

controller

def whatever
@user = User.find(u_id)
@user_details = @user.personal_details
end

or, just do this in the view

<%= @user.personal_details.first_name %>

On Nov 30, 3:44 am, Nadeesha M. <rails-mailing-l…@andreas-