Just want the id's back from the dynamic find

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.

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.

On 6/29/07, Mark G. [email protected] 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


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

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.

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.

On 7/1/07, unimatrixZxero [email protected] 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

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. [email protected]