Populate table from another table

I want to populate one table in a DB from another table so that the
user does not need to enter the same information twice.

I have a registration table that is populated when a user creates an
account. Later I have a form that uses a different table, but contains
most of the same info as the registration table. I would like that
table to be prepopulated by the registration table before the user gets
to the form so that they can just update/change that information
instead of having to enter it all over again.

What is the best way to do this?

Code and pointers to tutorials is always helpful.
Thanks in advance – K

If the two tables are actually storing the same data, I would say you
need
to consider a different database layout. But, I believe you are saying
that
you want to provide default values for the form, drawn from the users
table. If that is the case, I you pre-populate an in-memory form model
with
info from the users table, and send that to the form view. This will
then
have that default data in the web form when it is viewed by the user.
When
the form is submitted, you treat it as you would any other form. In
this
method, you simply provide default values in the form fields, not in the
database. Your controller might look something like this:

@viewer is the user that is logged in and viewing the page, perhaps

set in
a before_filter
def invitation
@invitation = Invitation.new
@invitation.address = @viewer.address
end

Hope that helps.

On 30 Nov 2006, at 23:58, Kim wrote:

What is the best way to do this?

Code and pointers to tutorials is always helpful.
Thanks in advance – K

Have a look at this post by Jamis B.:

http://weblog.jamisbuck.org/2006/11/7/don-t-be-afraid-of-harnessing-sql

Regards,
Andy S.

Let me explain a little more.

I have a user table that has_many pages which has_many page_modules.
There are different types of page_modules - an instructor_module , a
librarian _module, etc…

The user registers as either an instructor or a librarian - hence why I
have to store duplicate information in the user and the instructor/
librarian table.

Take this example: A librarian creates an account and fills out
information about themselves which is stored in the user table. They
create a page and then start
creating the page_modules. They come to the librarian _module. I want
the system to recognize that they are a librarian (per the role field
in the user table) and populate the librarian _module form from the
user table which will then be saved to the librarian _module.

If I was to use foreign keys linking the user table to the librarian
_module table instead of duplicating the data in two tables - how will
an instructor create the librarian _module or vice versa. Do you see my
point?

So again how can I preload data from one table to another table before
the forms view is displayed?

I would also accept help on my database scheme if I am way off base in
my thinking.

Thanks – K

I’m not quite sure I understand the data duplication problem in your
schema. But I think the solution you are looking for might be an
after_create hook in the User model. If you define a method in your
User
model named after_create, it will be run after the model is ‘created’,
in
other words, after it has been inserted into the database for the first
time. In this method, you might want to do something like:

class User < ActiveRecord::Base

def after_create
case self.role
when “librarian”
LibrarianModule.new do |mod|
# populate the mod object and save it
mod.user = self

mod.save!
end
when “instructor”
… # something similar
end
end

end

In this solution, creating a use automatically results in the creation
of a
corresponding LibrarianModule when the User model is first created.
Depending on you project, it might make more sense to use an
after_create
method in the LibrarianModule. Hopefully this helps.

jkit

I think the after_create method will work. Thanks.