# Retrieve data from two Tables and validate

**URL:** <https://www.ruby-forum.com/t/retrieve-data-from-two-tables-and-validate/121717>\
**Category:** Rails\
**Created:** [November 30, 2007, 6:11am UTC](https://www.ruby-forum.com/t/retrieve-data-from-two-tables-and-validate/121717 "2007-11-30T06:11:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 30, 2007, 6:11am UTC](https://www.ruby-forum.com/t/retrieve-data-from-two-tables-and-validate/121717/1 "2007-11-30T06:11:20Z")

</div>

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…

🙂 🙂 🙂 🙂 🙂 🙂 🙂

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 30, 2007, 7:05am UTC](https://www.ruby-forum.com/t/retrieve-data-from-two-tables-and-validate/121717/2 "2007-11-30T07:05:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 30, 2007, 9:44am UTC](https://www.ruby-forum.com/t/retrieve-data-from-two-tables-and-validate/121717/3 "2007-11-30T09:44:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 30, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/retrieve-data-from-two-tables-and-validate/121717/4 "2007-11-30T18:08:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 30, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/retrieve-data-from-two-tables-and-validate/121717/5 "2007-11-30T20:54:43Z")

</div>

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-
