Collect on a select helper

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,] },
{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,


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

<%= select( “bar”, “account”, Bar.find_by_sql( ‘SELECT * From bars where
bars.account > 0’ ).collect{ |p| [p.account,] },
{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’)