# Catch id from form and copy between objects

**URL:** <https://www.ruby-forum.com/t/catch-id-from-form-and-copy-between-objects/60469>\
**Category:** Rails\
**Created:** [January 9, 2006, 11:59am UTC](https://www.ruby-forum.com/t/catch-id-from-form-and-copy-between-objects/60469 "2006-01-09T11:59:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 9, 2006, 11:59am UTC](https://www.ruby-forum.com/t/catch-id-from-form-and-copy-between-objects/60469/1 "2006-01-09T11:59:13Z")

</div>

Hi all,

Not completely sure what I’m doing, but I need to get data from another  
table  
based on and id. Below is some breakpointer output:

irb\> @params  
=\> {“project”=\>{“project\_description”=\>“point2”,  
“project\_name”=\>“break2”},  
“action”=\>“create\_project”, “id”=\>{“collect”=\>“6”},  
“controller”=\>“adm/project”}

The one I want is the ‘6’ in “id”=\>{“collect”=\>“6”}

The irb output is right after the form submission. The following  
statements  
are executed

@contact = Contact.new  
@project = Project.new(@params[‘project’])  
@project\_contact = ProjectContact.new(@params[‘collect’])  
breakpoint

What needs to be done is catch some fields and copy them based on the  
id.

@project\_contact.firstname =  
@contact.find\_on\_id\_from\_form\_somehow.firstname

The “find\_on\_id\_from\_form\_somehow” needs to be replaced with a  
conditional  
find … i guess. Can somebody give me a pointer?

Thanx a lot.

Regards,

Gerard

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 9, 2006, 2:08pm UTC](https://www.ruby-forum.com/t/catch-id-from-form-and-copy-between-objects/60469/2 "2006-01-09T14:08:52Z")

</div>

> What needs to be done is catch some fields and copy them based on  
> the id.
> 
> @project\_contact.firstname =  
> @contact.find\_on\_id\_from\_form\_somehow.firstname
> 
> The “find\_on\_id\_from\_form\_somehow” needs to be replaced with a  
> conditional  
> find … i guess. Can somebody give me a pointer?

As a long time Lotus Notes developer, I know that “redundancy is  
good ;-)”, but in SQL land, is this really what you want? Is the  
firstname in @project\_contact really independent of the @contact? If  
not (that’s what I suspect) then you could have something like

Class Project  
belongs\_to :contact  
end

Class Contact  
has\_many :projects  
end

and in your code write something like:

@project = Project.new(@params…)  
@project.contact = Contact.find(@params[:collect])

and later

project contact firstname: \<%= @project.contact.firstname -%\>

sorry, if this is totally not what you wanted

cu jc

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 9, 2006, 10:00pm UTC](https://www.ruby-forum.com/t/catch-id-from-form-and-copy-between-objects/60469/3 "2006-01-09T22:00:26Z")

</div>

Jens-Christian,

There are two contacts tables:

company (hm) ↔ (bt) contacts  
project (ho) ↔ (bt) project\_contacts  
(ho might become hm)

project (ho) ↔ (bt) project\_products

When creating a new project I copy some of the fields from “contacts” to  
“project\_contacts”. The following would occcur with only the contacts  
table:  
When a contact is destroyed (resigns or starts working for a different  
company), the project is no longer visible and will generate errors,  
‘nill’  
issues I assume.

When I run the new\_project method a nice list is created (line 23) based  
on a  
sql query (line 21). When submitting the form and going to  
create\_project I  
get kinda lost. I’m not sure if line 29 is nessecary. And the line from  
the  
earlier email:

> > @contact.find\_on\_id\_from\_form\_somehow.firstname

Should be on line 36, but I’m still figuring out how to get the contents  
of a  
specific field of a specific table. I’m going to do this for several,  
but not  
all, fields. (probably: firstname, lastname, telnumber, email, company  
name  
and central company telnumber). Hope you can give me some pointers.

Thanx a lot!!

Regards,

Gerard.

```
 18 def new_project
 19 @project = Project.new
 20
 21 @contacts_list = Contact.find_by_sql('select t1.name,

```

t2.firstname, t2.lastname, t2.id from companies AS t1, contacts  
AS t2  
where t1.id = t2.company\_id order by t1.name’)  
22  
23 @contacts\_list = @contacts\_list.collect { |c| [ c.name + " -  
" +  
c.firstname + " " + c.lastname, c.id ] }  
24 render\_action ‘edit\_project’  
25  
26 end  
27  
28 def create\_project  
29 @contact = Contact.new  
30 @project = Project.new(@params[‘project’])  
31 @project\_contact = ProjectContact.new(@params[‘collect’])  
32  
33 @project.project\_contact = @project\_contact  
34 #breakpoint  
35 contact = Contact.find(@params[‘id’][‘collect’])  
36 @project.project\_contact[‘firstname’] = contact[“firstname”]  
37  
38 #@project.project\_contact.save  
39 if @project.save  
40 flash[:notice] = ‘Project was successfully created.’  
41 redirect\_to :action =\> ‘show\_project’, :id =\> @project.id  
42 else  
43 render :action =\> ‘edit\_project’  
44 end  
45 end

On Monday 09 January 2006 14:08, Jens-Christian F. tried to type  
something like:

> > What needs to be done is catch some fields and copy them based on  
> > the id.
> > 
> > @project\_contact.firstname =

> Class Project  
> @project.contact = Contact.find(@params[:collect])  
> InVisible GmbH, Langgrütstrasse 172, 8047 Zürich  
> Phone: +41 44 401 09 30  
> [http://www.invisible.ch](http://www.invisible.ch)[http://not.invisible.ch](http://not.invisible.ch)

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!
