# Problem: To retrive a specific column by method.find

**URL:** <https://www.ruby-forum.com/t/problem-to-retrive-a-specific-column-by-method-find/92416>\
**Category:** Rails\
**Created:** [April 19, 2007, 10:18am UTC](https://www.ruby-forum.com/t/problem-to-retrive-a-specific-column-by-method-find/92416 "2007-04-19T10:18:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [April 19, 2007, 10:18am UTC](https://www.ruby-forum.com/t/problem-to-retrive-a-specific-column-by-method-find/92416/1 "2007-04-19T10:18:43Z")

</div>

hi guys,  
the method all\_data retrive all data from a table  
def all\_data  
@output\_column = OutputColumn.find :all  
end

But I want to retrive a person\_name column to generate a list of all  
person…  
so how can i do it?

Thx in advanced.

Milinda

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [April 19, 2007, 11:33am UTC](https://www.ruby-forum.com/t/problem-to-retrive-a-specific-column-by-method-find/92416/2 "2007-04-19T11:33:47Z")

</div>

Eric Milinda wrote:

> hi guys,  
> the method all\_data retrive all data from a table  
> def all\_data  
> @output\_column = OutputColumn.find :all  
> end
> 
> But I want to retrive a person\_name column to generate a list of all  
> person…  
> so how can i do it?

I have thought that the previous solution can be achieved by the  
following code.

def fil\_string  
sql = ActiveRecord::Base.connection()  
filter\_string=’ ’  
sql.begin\_db\_transaction  
column\_ids = sql.execute(“SELECT columnid FROM output\_columns”)  
column\_ids.each do |column\_id|  
filter\_string=filter\_string +" columnid=’"+column\_id+"’"  
end  
sql.commit\_db\_transaction  
end

## but in the solution Error: can not convert array to string at line – filter\_string=filter\_string +" columnid=’"+column\_id+"’" "

Please take a hand in my code.

thx.

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [April 19, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/problem-to-retrive-a-specific-column-by-method-find/92416/3 "2007-04-19T14:36:33Z")

</div>

ActiveRecord works at the row (aka record) level, not the column  
level. That’s not to say that you can’t just get a single column of  
data back from the database, you can, but you won’t have all the  
attributes if you do this so just keep that in mind. you’ll probably  
want to map them to an array

try something like:

class User \< ActiveRecord::Base  
def self.names  
# find all records, then map name attributes to an array  
find(:all, :select =\> “name”).map(&:name)  
end  
end

then just do

@names = User.names

in your controller methods where you need it

btw

.map(&:name)

is shorthand for

.map { |x| x.name }
