# HOWTO: Combine fields from 2 two tables in 1 object

**URL:** <https://www.ruby-forum.com/t/howto-combine-fields-from-2-two-tables-in-1-object/90251>\
**Category:** Rails\
**Created:** [January 5, 2006, 1:51pm UTC](https://www.ruby-forum.com/t/howto-combine-fields-from-2-two-tables-in-1-object/90251 "2006-01-05T13:51:17Z")\
**Posts on this page:** 7\
**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 5, 2006, 1:51pm UTC](https://www.ruby-forum.com/t/howto-combine-fields-from-2-two-tables-in-1-object/90251/1 "2006-01-05T13:51:17Z")

</div>

Hi all,

For a dropdownlist (showing "Company - FirstName Lastname’) I want to  
fill an  
object @project\_contacts with “Name” from table Companies and  
“Firstname” and  
“Lastname” from table contacts. Any idea?

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 5, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/howto-combine-fields-from-2-two-tables-in-1-object/90251/2 "2006-01-05T17:44:17Z")

</div>

Hi all,

Watery eyes … Nevermind the previous. I don’t see that working for me.

This is what I want. I create a new project, and want to copy some  
contact and  
company info into a project table.

I don’t see a link (selected via a view) between to completely seperate  
tables. Let alone get data from one to the other.

Please help?

Gerard.

On Thursday 05 January 2006 13:49, Gerard tried to type something like:

> Hi all,
> 
> For a dropdownlist (showing "Company - FirstName Lastname’) I want to fill  
> an object @project\_contacts with “Name” from table Companies and  
> “Firstname” and “Lastname” from table contacts. Any idea?
> 
> 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 5, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/howto-combine-fields-from-2-two-tables-in-1-object/90251/3 "2006-01-05T17:44:24Z")

</div>

assuming certain relationships between projects/contacts/companies

projects (hm) \<–\> (bt) contacts (bt) \<–\> (hm) companies

controller:

@project.find(1)  
@project\_contacts = @project.contacts.collect { |c| [ c.company.name + "

- "

- c.first\_name + " " + c.last\_name, c.id ] }

# @project\_contacts =\> [[ “Acme -Bob S.”, 1], ["MegaCorp - Jane

Doe",  
2], … ]

view:

\<%= select(“project”, “contact\_id”, @project\_contacts ) %\>

---

<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 5, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/howto-combine-fields-from-2-two-tables-in-1-object/90251/4 "2006-01-05T17:44:43Z")

</div>

Chris,

Hurtfully I must state being a newbie can slow things down … 🙂

Cudo’s on your solution. It hits the nail on the head. Looking at your  
solution … .would it be wise to buy the pragmatic ruby book. Or do I  
just  
need to keep going knowing I’ll get better?

Anyway …

> projects (hm) ↔ (bt) contacts (bt) ↔ (hm) companies  
> Very close. This is true:(bt) contacts (bt) ↔ (hm) companies

And there is: projects (ho) ↔ (bt) project\_contacts

The reason is that when a contact resigns (is deleted) I still want to  
be able  
to view closed projects. Herewith avoiding the (for me) infamous ‘nil’  
errors.

Since there’s no relationship between projects & project\_contacts on one  
side  
and contacts on the other. Looks like I need to do a second object fill  
when  
saving the new project. I can then use the ‘"c.id’ that comes back from  
@project\_contacts and use that id to gather the info for storing it. Or  
is  
that info still avalaible in @project.contacts.collect ?

Thanx so much for helping me out.

Gerard

> > :wq!
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“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 13, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/howto-combine-fields-from-2-two-tables-in-1-object/90251/5 "2006-01-13T16:36:53Z")

</div>

Hi,

We have experienced some problems with  
ActiveRecord::Base class of Ruby-Rails. We are  
building a web-application based on the Ruby-rails  
framework and the web-application needs to access to  
difference databases, so we do not pre-define our  
database accesses in the database.yml file. In fact,  
we are using the  
ActiveRecord::Base.establish\_connection() to connect  
to our database, the function works perfectly.  
However, while we try to disconnect our database  
connection with  
ActiveRecord::Base.remove\_connection(), we find out  
that the connection is not closed (the function seems  
not to be working perfectly) and we are still  
connected to the database and able to access to the  
tables.

Can you please tell us, is there something we did  
wrong? or it is a known problem?  
and what should we do to disconnect a database  
connection ?

The using version ruby-rails are :  
Rails 0.14.2 and ruby 1.8.3 (2005-09-21)  
[x86\_64-linux]  
We are running on Linux KDE 3.4, and our database  
engine is postgresql

The following is a code example:  
####################################################################################

class GenTableAs \< ActiveRecord::Base  
end

class GentestController \< ApplicationController

```
def index
	GenTableAs.establish_connection(:adapter =>

```

“postgresql”,  
:host =\> “1.2.3.4”,  
:port =\> 5432,  
:database =\> “gendb”,  
:username =\> “dbuser”,  
:password =\> “dbuser”)  
@connected1 = GenTableAs.connection()  
@removedConnection = GenTableAs.remove\_connection()

```
	@connected2 = GenTableAs.connection() # should be

```

nil here  
obj1 = GenTableAs.new  
obj1[“colaa”] = 100  
obj1[“colab”] = “100”  
obj1.save # should have raised exception or error  
here  
end  
end  
####################################################################################  
the output of index

@connected1:

#\<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x2aaaac138930  
@runtime=0.0189187526702881,  
@connection=#PGconn:0x2aaaac138980,  
@logger=#\<Logger:0x2aaaac2e7fd8  
@default\_formatter=#\<Logger::Formatter:0x2aaaac2e7dd0  
@datetime\_format=nil\>, @progname=nil,  
@logdev=#\<Logger::LogDevice:0x2aaaac2e7ce0  
@shift\_size=1048576, @shift\_age=0,  
@filename=“./log/development.log”,  
@mutex=#\<Logger::LogDevice::LogDeviceMutex:0x2aaaac2e7c90  
@mon\_waiting\_queue=[], @mon\_entering\_queue=[],  
@mon\_count=0, @mon\_owner=nil\>,  
@dev=#\<File:./log/development.log\>\>, @level=0,  
@formatter=nil\>\>

removed connection:

:password: dbuser  
:database: gendb  
:host: 1.2.3.4  
:port: 5432  
:adapter: postgresql  
:username: dbuser

@connected2:

#\<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x2aaaac138930  
@runtime=0.0189187526702881,  
@connection=#PGconn:0x2aaaac138980,  
@logger=#\<Logger:0x2aaaac2e7fd8  
@default\_formatter=#\<Logger::Formatter:0x2aaaac2e7dd0  
@datetime\_format=nil\>, @progname=nil,  
@logdev=#\<Logger::LogDevice:0x2aaaac2e7ce0  
@shift\_size=1048576, @shift\_age=0,  
@filename=“./log/development.log”,  
@mutex=#\<Logger::LogDevice::LogDeviceMutex:0x2aaaac2e7c90  
@mon\_waiting\_queue=[], @mon\_entering\_queue=[],  
@mon\_count=0, @mon\_owner=nil\>,  
@dev=#\<File:./log/development.log\>\>, @level=0,  
@formatter=nil\>\>

Thanks you very much !!!

Saiho

* * *

Do You Yahoo!?  
Tired of spam? Yahoo! Mail has the best spam protection around

> **[\[object](https://mail.yahoo.com/)**
>
> \[object

---

<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 16, 2006, 5:22pm UTC](https://www.ruby-forum.com/t/howto-combine-fields-from-2-two-tables-in-1-object/90251/6 "2006-01-16T17:22:25Z")

</div>

Hi,

I wish to know where and when exactly the rails  
connects to a database when we create a subclass of  
ActiveReocrd::base ? And How ActiveRecord::base work  
exactly?

I took a look on the classes ConnectionSpecification,  
PostgreSQLAdapter(we are using Postgresql) and  
AbstractAdapter. I cannot figure out function calling  
stack. Can someone can tell me how the all these  
functions is called?

As I understand the connections is stored in the  
@@defined\_connections od the class  
ConnectionSpecification. And when we “remove” the  
connection, the function “remove connection” only  
remove the connection from the variable  
@@defined\_connections, but it does not “disconnect”  
the connection between rails and the database, So I  
wonder when the rails connects to the database, and  
when rails decides to disconnect, and how it is done?

Thanks you very much

Saiho

* * *

Do You Yahoo!?  
Tired of spam? Yahoo! Mail has the best spam protection around

> **[\[object](https://mail.yahoo.com/)**
>
> \[object

---

<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 6, 2006, 12:32pm UTC](https://www.ruby-forum.com/t/howto-combine-fields-from-2-two-tables-in-1-object/90251/7 "2006-01-06T12:32:16Z")

</div>

Chris,

Having chewed on the line bewol some more

@project\_contacts = @contacts\_list.collect { |c| [ c.name + " - " +  
c.firstname + " " + c.lastname, c.id ] }

I do need the c.id in the end. but not displayed in the dropdownlist.  
And it’s  
shown like this now: “Company - FirstName Lastname3”  
The “3” being the id of the contact. Do i need to fill the  
@project\_contacts  
in a different way, or address the select statement data input in a  
different  
way? What would be your choice for ease of use?

Thanx,

Regards.

On Thursday 05 January 2006 14:34, Chris H. tried to type something  
like:

> 2], … ]
> 
> > object @project\_contacts with “Name” from table Companies and “Firstname”
> > 
> > My $Grtz =~ Gerard;  
> > ~
> > 
> > :wq!
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“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!
