# Collect on a select helper

**URL:** <https://www.ruby-forum.com/t/collect-on-a-select-helper/113392>\
**Category:** Rails\
**Created:** [August 30, 2007, 5:32pm UTC](https://www.ruby-forum.com/t/collect-on-a-select-helper/113392 "2007-08-30T17:32:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 30, 2007, 5:32pm UTC](https://www.ruby-forum.com/t/collect-on-a-select-helper/113392/1 "2007-08-30T17:32:07Z")

</div>

I have two tables in a database: bars and accounts. (bars isn’t what you  
think, but that’s anoher story). Anyway, an accounts has\_many bars and  
bars belongs\_to account. In my bars table their is an id and an account  
column. What I am trying to do is to pull out the account variabel from  
the bars database and am having some issues. I get this error:

RuntimeError: ERROR C42703 Mcolumn accounts.bar\_id does not exist  
Fparse\_func.c L1036 Runknown\_attribute: SELECT \* FROM accounts WHERE  
(accounts.bar\_id = 150991) LIMIT 1

Odviously, it is getting confused about which table it should be looking  
in, but to be honest, I’m looking at my code and it looks pretty sound  
and it should work since I have written this exact code for various  
things many, many times in the past. Here is the select:

\<%= select( “bar”, “account”, Bar.find\_by\_sql( ‘SELECT \* From bars where  
bars.account \> 0’ ).collect{ |p| [p.account, p.id] },  
{include\_blank =\> false } ) %\>

Unless I am mistaken, the p in the collect is an object of bar correct?  
It is here that I believe all my problems are occuring. Thanks,

-Shandy

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 30, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/collect-on-a-select-helper/113392/2 "2007-08-30T18:03:14Z")

</div>

Figured it out. My relationships where in error in my models. In my bar  
model I had has\_one :account when it should have read \_belongs\_to  
:account.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 30, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/collect-on-a-select-helper/113392/3 "2007-08-30T22:30:51Z")

</div>

> \<%= select( “bar”, “account”, Bar.find\_by\_sql( ‘SELECT \* From bars where  
> bars.account \> 0’ ).collect{ |p| [p.account, p.id] },  
> {include\_blank =\> false } ) %\>

Rails eliminates the need to degenerate your code to SQL fragments for  
most things.

You could write this query more cleanly. Instead of:

Bar.find\_by\_sql( ‘SELECT \* From bars where bars.account\_id \> 0’ )

Do this:

Bar.find(:all, :conditions =\> ‘account\_id \> 0’)

Cheers,  
Ryan
