# Insert select statements from different database

**URL:** <https://www.ruby-forum.com/t/insert-select-statements-from-different-database/143037>\
**Category:** Rails\
**Created:** [July 14, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/insert-select-statements-from-different-database/143037 "2008-07-14T18:20:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![monsterag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@monsterag](https://www.ruby-forum.com/u/monsterag)\
**Post date:** [July 14, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/insert-select-statements-from-different-database/143037/1 "2008-07-14T18:20:43Z")

</div>

Hello,  
i want to insert a select field into my form.  
The fields of the select-field should come from a database that is  
created in a different modell in the same project.  
I donÂ´t need relations simply the content from one field in the table  
that should populate the select-field.  
How can i do this?  
The selected item from the select-field will then be saved amoung other  
fields in a second database  
tia for a hint!

---

<div class="post-metadata">

**Author:** ![monsterag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@monsterag](https://www.ruby-forum.com/u/monsterag)\
**Post date:** [July 14, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/insert-select-statements-from-different-database/143037/2 "2008-07-14T19:40:30Z")

</div>

Thanks, i know the helper method, but i do not know how to reference to  
the database…  
assume i am in the model “a” in the view “a.new.html.erb”  
how do i implement the creation of the object in the controller of “a”  
to access the datas from model “b”  
So as i am a beginner, there seems to be a lack in the knowledge of  
ruby…  
so any help would be useful.  
tia

---

<div class="post-metadata">

**Author:** ![monsterag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@monsterag](https://www.ruby-forum.com/u/monsterag)\
**Post date:** [July 14, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/insert-select-statements-from-different-database/143037/3 "2008-07-14T18:55:24Z")

</div>

On Jul 14, 12:20 pm, Einar K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> –  
> Posted viahttp://www.ruby-forum.com/.

Hi Einar,

If I understand your question correctly, you are looking for the  
collection\_select helper. Here is an example I have in my current  
project:  
\<%= f.collection\_select(:user\_id, @users, :id, :name) %\>

This helper method provides the opportunity to present parameters from  
an unrelated object to the user for selection. Be sure to create the  
object in the controller method.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![monsterag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@monsterag](https://www.ruby-forum.com/u/monsterag)\
**Post date:** [July 14, 2008, 8:22pm UTC](https://www.ruby-forum.com/t/insert-select-statements-from-different-database/143037/4 "2008-07-14T20:22:46Z")

</div>

I see. Again, assuming I understand your question, in your controller  
you have a method “def new”. In that method you probably have a  
statement something like this:  
@a = a.new  
You would add a second line:  
@b = b.find(:all)  
This will create an object of the b model class, which can then be  
used in the a/new.html.erb view.

Hope this helps.

On Jul 14, 1:40 pm, Einar K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
