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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 6/29/07, Mark Gargan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Moises Deniz 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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Gargan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 7/1/07, unimatrixZxero <unimatrixZxero-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > > 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 Gargan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > 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. > > > > -- > > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---