# ActiveRecord, Find that return only one column

**URL:** <https://www.ruby-forum.com/t/activerecord-find-that-return-only-one-column/183534>\
**Category:** Rails\
**Created:** [February 16, 2010, 7:01am UTC](https://www.ruby-forum.com/t/activerecord-find-that-return-only-one-column/183534 "2010-02-16T07:01:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 16, 2010, 7:01am UTC](https://www.ruby-forum.com/t/activerecord-find-that-return-only-one-column/183534/1 "2010-02-16T07:01:57Z")

</div>

Hello,  
Is it possible to make a “join” on an array of objects?  
If not, is is possible to return only one column with the find method?

#method  
def self.top\_3\_words  
Search.find(:all,:limit=\>3, :order =\>“count desc”)  
end

#View  
\<% if !@top\_search.blank? %\>  
\<%= @top\_search.word.join(",") %\> ???  
\<% else %\>

No search yet!

\<% end %\>

Greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 16, 2010, 7:14am UTC](https://www.ruby-forum.com/t/activerecord-find-that-return-only-one-column/183534/2 "2010-02-16T07:14:54Z")

</div>

On Tue, 2010-02-16 at 07:01 +0100, Greg Ma wrote:

> \<% if !@top\_search.blank? %\>  
> \<%= @top\_search.word.join(",") %\> ???  
> \<% else %\>
> 
> No search yet!
> 
> \<% end %\>

* * *

not sure what you’re getting at but

- concatenates two arrays

\<\< pushes an object on to the end of an array

> > test1 = Array.new  
> > =\> []
> 
> > test2 = [“5”]  
> > =\> [“5”]
> 
> > test1 + test2  
> > =\> [“5”]
> 
> > test1 \<\< “6”  
> > =\> [“6”]
> 
> > test1 + test2  
> > =\> [“6”, “5”]

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 16, 2010, 7:23am UTC](https://www.ruby-forum.com/t/activerecord-find-that-return-only-one-column/183534/3 "2010-02-16T07:23:06Z")

</div>

Assuming the followings:  
- Your Search model have a column “word”  
- @top\_search is a array with 3 latest search result, where each  
element have a :word  
- You want to join words from these 3 search objects

Then you just can do as following:  
@top\_search.map(&:word).join(’,’)

Hope it will work if my assumptions are correct 🙂

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 16, 2010, 7:24am UTC](https://www.ruby-forum.com/t/activerecord-find-that-return-only-one-column/183534/4 "2010-02-16T07:24:08Z")

</div>

Craig W. wrote:

> On Tue, 2010-02-16 at 07:01 +0100, Greg Ma wrote:
> 
> > \<% if !@top\_search.blank? %\>  
> > \<%= @top\_search.word.join(",") %\> ???  
> > \<% else %\>
> > 
> > No search yet!
> > 
> > \<% end %\>
> 
> * * *
> 
> not sure what you’re getting at but
> 
> - concatenates two arrays
> 
> \<\< pushes an object on to the end of an array
> 
> > > test1 = Array.new  
> > > =\> []
> > 
> > > test2 = [“5”]  
> > > =\> [“5”]
> > 
> > > test1 + test2  
> > > =\> [“5”]
> > 
> > > test1 \<\< “6”  
> > > =\> [“6”]
> > 
> > > test1 + test2  
> > > =\> [“6”, “5”]

This cannot work in my case as my array contains Tags object.  
I’d need to access each element to access the property i want to display

> Craig
> 
> –  
> This message has been scanned for viruses and  
> dangerous content by MailScanner, and is  
> believed to be clean.

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 16, 2010, 7:24am UTC](https://www.ruby-forum.com/t/activerecord-find-that-return-only-one-column/183534/5 "2010-02-16T07:24:46Z")

</div>

Samiron Rony wrote:

> Assuming the followings:  
> - Your Search model have a column “word”  
> - @top\_search is a array with 3 latest search result, where each  
> element have a :word  
> - You want to join words from these 3 search objects
> 
> Then you just can do as following:  
> @top\_search.map(&:word).join(’,’)
> 
> Hope it will work if my assumptions are correct 🙂

Great thanks 🙂

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 16, 2010, 7:33am UTC](https://www.ruby-forum.com/t/activerecord-find-that-return-only-one-column/183534/6 "2010-02-16T07:33:04Z")

</div>

You can also do something like Search.find(:all, :select =\>  
“word”, :limit=\>3, :order =\>“count desc”) if you only need the word  
column from each record and want to be a little easier on memory.

Also, be careful with the Symbol#to\_proc shorthand ( map(&:word) )  
with large arrays, as it is pretty CPU-inefficient in Rails from what  
I understand.

[http://blog.hasmanythrough.com/2006/3/7/symbol-to-proc-shorthand](http://blog.hasmanythrough.com/2006/3/7/symbol-to-proc-shorthand)  
(check the comments)

Jarin U.  
Robot Mode LLC
