ActiveRecord - find


What I would like to do is select a single column out of my model, say
the column is called my_column, and store the result in an array,
which it should be, right?

For example, if I do:
@my_arr = Model.find(:all, :select => “my_column”)

This should fill @my_arr with all the values from “my_column”, right?

So then I can access the data by some_other_variable = @my_arr[0], or
do I have to reference this as an object and do something like
some_other_variable = @my_arr.my_column?

Sorry, having a hard time getting this to work.

Thanks for your help!

I also tried this, and still nothing…any ideas?

Model.find(:all, :select => “my_column”, :order => “enterdate
DESC”).each do |col|

pete wrote:

I also tried this, and still nothing…any ideas?

Model.find(:all, :select => “my_column”, :order => “enterdate
DESC”).each do |col|

this should work, at least if @arr was initialized as an array:
@arr = []

in your first post you could have used the form @my_arr.my_column
the finders always return arrays of objects of the model class,
never pure arrays of strings, otherwise you would lose all the
class functionality

good question :slight_smile:
had to try that myself, and no, they’re not

did you get any errors in development.log?
from your questions i assume, that you don’t
know that very helpful item, so here a short

in your project directory in folder log you’ll
find a file development.log, where rails writes
quite a lot of stuff

open it, delete everything in it and then run
the query. any errors will appear in it
(and if you access you code from a browser you’ll
get a lot of info like which controller/action was
called with which params

with “text”
you can write there directly for debugging


Model.find(:all, :select => “my_column”).each do |col| "foo: #{col.my_column}

should give you a nice list or the errors

another way to try queries like that is the command

this opens irb with all the rails stuff preloaded, so
you can run queries directly

That’s a great tip, thanks, I didn’t know that was there.

As it turns out, I upcased everything and it worked fine. In :select,
I can leave it any case I want, however when I reference the object, I
have to use capitals, since that is how it was setup in the DB.

Thanks again for the logging info, that will help tremendously!!!

On Feb 1, 3:19 pm, Thorsten M. [email protected]

I’m just not getting anything.

Is the column name case sensitive? For example, in MySQL it is
“MY_COLUMN”, versus “my_column”.

On Feb 1, 3:01 pm, Thorsten M. [email protected]