# Just want the id's back from the dynamic find

**URL:** <https://www.ruby-forum.com/t/just-want-the-ids-back-from-the-dynamic-find/108145>\
**Category:** Rails\
**Created:** [June 29, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/just-want-the-ids-back-from-the-dynamic-find/108145 "2007-06-29T13:07:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bowenmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@bowenmark](https://www.ruby-forum.com/u/bowenmark)\
**Post date:** [June 29, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/just-want-the-ids-back-from-the-dynamic-find/108145/1 "2007-06-29T13:07:24Z")

</div>

Hey folks,

```
   in the rdoc for the ActiveRecord Base class they indicate how to

```

retrieve records dynamically using find\_by\_XYZ, however, what if you  
don’t want the whole record back? Do all the find methods return arrays  
of the object or is it possible for a find method to say return an array  
of one of the columns in the object.

I’ve an Alert class and a corresponding Alert table in the DB. There’s a  
message column, ‘message’ and a creation time column ‘creation’.

I was hoping to retrieve all the messages that occured in the last 30  
mins but the following code seems invalid…

messages = []  
messages = Alert.find\_message(:all, :condition =\>“creation \> sysdate -  
interval ‘30’ minute”)

I don’t want to go retrieving the entire Alert object from the DB solely  
for one column as in cases where there are hundreds of messages, object  
creation would be a big factor I think.

If it’s poss could anyone give me some idea as to how to go about it.

thanks,  
Mark.

---

<div class="post-metadata">

**Author:** ![bowenmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@bowenmark](https://www.ruby-forum.com/u/bowenmark)\
**Post date:** [June 29, 2007, 1:36pm UTC](https://www.ruby-forum.com/t/just-want-the-ids-back-from-the-dynamic-find/108145/2 "2007-06-29T13:36:27Z")

</div>

Model finders are designed to retrieve model objects…  
You can try adding a select option, something like this:

messages = Alert.find(:all, :select=\> ‘message’, :condition =\>"creation

> sysdate - interval ‘30’ minute")

The cost of creating objects could be not taken into account… maybe

> ```
> in the rdoc for the ActiveRecord Base class they indicate how to
> 
> ```
> 
> retrieve records dynamically using find\_by\_XYZ, however, what if you  
> don’t want the whole record back? Do all the find methods return arrays  
> of the object or is it possible for a find method to say return an array  
> of one of the columns in the object.
> 
> I’ve an Alert class and a corresponding Alert table in the DB. There’s a  
> message column, ‘message’ and a creation time column ‘creation’.
> 
> I was hoping to retrieve all the messages that occured in the last 30  
> mins but the following code seems invalid…
> 
> messages = []  
> messages = Alert.find\_message(:all, :condition =\>“creation \> sysdate -  
> interval ‘30’ minute”)
> 
> I don’t want to go retrieving the entire Alert object from the DB solely  
> for one column as in cases where there are hundreds of messages, object  
> creation would be a big factor I think.

---

<div class="post-metadata">

**Author:** ![bowenmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@bowenmark](https://www.ruby-forum.com/u/bowenmark)\
**Post date:** [June 29, 2007, 1:42pm UTC](https://www.ruby-forum.com/t/just-want-the-ids-back-from-the-dynamic-find/108145/3 "2007-06-29T13:42:13Z")

</div>

On 6/29/07, Mark G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey folks,
> 
> ```
> in the rdoc for the ActiveRecord Base class they indicate how to
> 
> ```
> 
> retrieve records dynamically using find\_by\_XYZ, however, what if you  
> don’t want the whole record back? Do all the find methods return arrays  
> of the object or is it possible for a find method to say return an array  
> of one of the columns in the object.

the connection adapters have a #select\_values method

[http://rails.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#M000727](http://rails.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#M000727)

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![bowenmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@bowenmark](https://www.ruby-forum.com/u/bowenmark)\
**Post date:** [June 29, 2007, 2:54pm UTC](https://www.ruby-forum.com/t/just-want-the-ids-back-from-the-dynamic-find/108145/4 "2007-06-29T14:54:36Z")

</div>

Thanks a million Rick that worked spot on alright and there was no  
object creation. Alas the code now has a select statement right slap  
bang in the middle of it. It was the kinda thing I was trying to avoid.  
Don’t get me wrong I really appreciate the answer and it means I could  
move on, I was just hoping the  
Alert.find(:all, :select =\> “message”… line would just return an array  
of the types in the :select as opposed to creating full Alert objects.

Thanks again for all the help folks,  
Mark.

---

<div class="post-metadata">

**Author:** ![bowenmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@bowenmark](https://www.ruby-forum.com/u/bowenmark)\
**Post date:** [June 29, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/just-want-the-ids-back-from-the-dynamic-find/108145/5 "2007-06-29T14:51:10Z")

</div>

Moises D. wrote:

> Model finders are designed to retrieve model objects…  
> You can try adding a select option, something like this:
> 
> messages = Alert.find(:all, :select=\> ‘message’, :condition =\>"creation
> 
> > sysdate - interval ‘30’ minute")
> 
> The cost of creating objects could be not taken into account… maybe

Thanks a million Moises. The sql run against the db was

SELECT MESSAGE  
FROM alerts  
WHERE (creation \> sysdate - interval ‘30’ minute)

However the object is still being created in order to house the message  
attribute.

> > ```
> > in the rdoc for the ActiveRecord Base class they indicate how to
> > 
> > ```
> > 
> > retrieve records dynamically using find\_by\_XYZ, however, what if you  
> > don’t want the whole record back? Do all the find methods return arrays  
> > of the object or is it possible for a find method to say return an array  
> > of one of the columns in the object.
> > 
> > I’ve an Alert class and a corresponding Alert table in the DB. There’s a  
> > message column, ‘message’ and a creation time column ‘creation’.
> > 
> > I was hoping to retrieve all the messages that occured in the last 30  
> > mins but the following code seems invalid…
> > 
> > messages = []  
> > messages = Alert.find\_message(:all, :condition =\>“creation \> sysdate -  
> > interval ‘30’ minute”)
> > 
> > I don’t want to go retrieving the entire Alert object from the DB solely  
> > for one column as in cases where there are hundreds of messages, object  
> > creation would be a big factor I think.

---

<div class="post-metadata">

**Author:** ![bowenmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@bowenmark](https://www.ruby-forum.com/u/bowenmark)\
**Post date:** [July 1, 2007, 1:44pm UTC](https://www.ruby-forum.com/t/just-want-the-ids-back-from-the-dynamic-find/108145/6 "2007-07-01T13:44:15Z")

</div>

On 7/1/07, unimatrixZxero [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> wrote:
> 
> > –  
> > Posted viahttp://www.ruby-forum.com/.

In my own personal, very limited benchmarking, I have had some very  
nasty  
surprises when I’ve included an array creation inside a loop. The  
results  
over a few thousand iterations can be significant in my experiments.

Daniel

---

<div class="post-metadata">

**Author:** ![bowenmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@bowenmark](https://www.ruby-forum.com/u/bowenmark)\
**Post date:** [July 1, 2007, 4:18am UTC](https://www.ruby-forum.com/t/just-want-the-ids-back-from-the-dynamic-find/108145/7 "2007-07-01T04:18:37Z")

</div>

Of course you know. Arrays are objects too. Like everything else in  
Ruby. btw I can’t remember where I found it, I thin, on a Joyent  
podcast, a few weeks back, but object creation in Ruby supposedly  
takes only 145 nano-seconds. I don’t know if that applies only to the  
bare Object.new constructor but I don’t see why dealing with your  
message objects gives you “a hard time”. Anywho, just my little vent.

On Jun 29, 2:54 pm, Mark G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
