# SQL and select()

**URL:** <https://www.ruby-forum.com/t/sql-and-select/181276>\
**Category:** Rails\
**Created:** [January 8, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/sql-and-select/181276 "2010-01-08T17:19:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quijote70](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/0ea827/32.png) [@Quijote70](https://www.ruby-forum.com/u/Quijote70)\
**Post date:** [January 8, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/sql-and-select/181276/1 "2010-01-08T17:19:33Z")

</div>

I don’t know where I make the mistake

if i have an sql in the controller like this:

```
@sql = "select result.lang_id as id,pv_langs.name as name from

```

((SELECT pv\_lang\_id as lang\_id FROM pv\_castings)  
UNION (SELECT sv\_lang\_id as lang\_id FROM pv\_castings)) as result  
join pv\_langs on result.lang\_id = pv\_langs.id GROUP BY  
result.lang\_id"  
@langs = ActiveRecord::Base.connection.execute (@sql)

then in the view i want to create a select box

```
<%= select("lang", "lang_id", @langs.map {|p| [p.name, p.id] })

```

%\>

can you explain why it dont work?

Sergio

---

<div class="post-metadata">

**Author:** ![Quijote70](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/0ea827/32.png) [@Quijote70](https://www.ruby-forum.com/u/Quijote70)\
**Post date:** [January 8, 2010, 5:58pm UTC](https://www.ruby-forum.com/t/sql-and-select/181276/2 "2010-01-08T17:58:46Z")

</div>

On Jan 8, 4:17Â pm, Quijote70 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Â Â @langs = ActiveRecord::Base.connection.execute (@sql)
> 
> then in the view i want to create a select box
> 
> Â Â \<%= select(“lang”, “lang\_id”, @langs.map {|p| [p.name, p.id] })  
> %\>
> 
> can you explain why it dont work?

execute returns a database specific result set object (eg  
Mysql::Result for mysql etc.). You probably want something like  
select\_all that returns an array of hashes

Fred

---

<div class="post-metadata">

**Author:** ![Quijote70](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/0ea827/32.png) [@Quijote70](https://www.ruby-forum.com/u/Quijote70)\
**Post date:** [January 8, 2010, 6:08pm UTC](https://www.ruby-forum.com/t/sql-and-select/181276/3 "2010-01-08T18:08:54Z")

</div>

> then in the view i want to create a select box
> 
> ```
> <%= select("lang", "lang_id", @langs.map {|p| [p.name, p.id] })
> 
> ```
> 
> %\>

Try this :-

\<%= select(“lang”, “lang\_id”, @langs.map {|p| [p[‘name’], p[‘id’] ] })  
%\>

or

\<%= select(“lang”, “lang\_id”, @langs.map {|p| [p[1], p[0] ] }) %\>

---

<div class="post-metadata">

**Author:** ![Quijote70](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/0ea827/32.png) [@Quijote70](https://www.ruby-forum.com/u/Quijote70)\
**Post date:** [January 11, 2010, 1:45pm UTC](https://www.ruby-forum.com/t/sql-and-select/181276/4 "2010-01-11T13:45:29Z")

</div>

Perfect, yes I was concentred to the map and I didnt look the execute

On Fri, 8 Jan 2010 08:57:09 -0800 (PST), Frederick C.
