I have a table named "Projects" which has a column "name". I want to get a string array to represent all the name values. In the controller, I can use this code "@projects = Project.find(:all)" to get all the projects but how to convert it to the string array? thanks. -- 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 Fri, Dec 26, 2008 at 3:00 AM, Zhao Yi <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I have a table named "Projects" which has a column "name". I want to get > a string array to represent all the name values. In the controller, I > can use this code "@projects = Project.find(:all)" to get all the > projects but how to convert it to the string array? >Maybe a map will do the trick @project_names = Project.all.map { |p| p.name } -- Gabriel Laskar <bibi.skuk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Maybe this is a bit off topic... I never knew you could use syntax like Project.all. When was this added to the language/framework? On Thu, Dec 25, 2008 at 9:28 PM, Gabriel Laskar <bibi.skuk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Fri, Dec 26, 2008 at 3:00 AM, Zhao Yi > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > I have a table named "Projects" which has a column "name". I want to get > > a string array to represent all the name values. In the controller, I > > can use this code "@projects = Project.find(:all)" to get all the > > projects but how to convert it to the string array? > > > > Maybe a map will do the trick > > @project_names = Project.all.map { |p| p.name } > > > -- > Gabriel Laskar <bibi.skuk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Gabriel Laskar wrote:> Maybe a map will do the trick > > @project_names = Project.all.map { |p| p.name } > > > -- > Gabriel Laskar <bibi.skuk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>Thanks, it works. -- 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 -~----------~----~----~----~------~----~------~--~---
I think it''s been around since Rails 2.1 Project.first Project.last Project.all all work. On Dec 25, 11:12 pm, "James Englert" <englert.ja...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Maybe this is a bit off topic... I never knew you could use syntax like > Project.all. When was this added to the language/framework? > > On Thu, Dec 25, 2008 at 9:28 PM, Gabriel Laskar <bibi.s...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Fri, Dec 26, 2008 at 3:00 AM, Zhao Yi > > <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > I have a table named "Projects" which has a column "name". I want to get > > > a string array to represent all the name values. In the controller, I > > > can use this code "@projects = Project.find(:all)" to get all the > > > projects but how to convert it to the string array? > > > Maybe a map will do the trick > > > @project_names = Project.all.map { |p| p.name } > > > -- > > Gabriel Laskar <bibi.s...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
A more efficient way is to do this is: @project_names = Project.all(:select => ''name'').map(&:name) On Dec 25, 11:44 pm, Zhao Yi <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Gabriel Laskar wrote: > > Maybe a map will do the trick > > > @project_names = Project.all.map { |p| p.name } > > > -- > > Gabriel Laskar <bibi.s...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > Thanks, it works. > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan wrote:> I think it''s been around since Rails 2.1 > > Project.first > Project.last > Project.all > > all work.I think the first last and all refers to rows. I want to select columns. -- 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 -~----------~----~----~----~------~----~------~--~---
Ryan wrote:> A more efficient way is to do this is: > > @project_names = Project.all(:select => ''name'').map(&:name)Yes the map is what I am looking for. thanks. -- 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 -~----------~----~----~----~------~----~------~--~---
Mohit Sindhwani
2008-Dec-26 05:27 UTC
Re: How to get one columns as an array from database
Zhao Yi wrote:> Ryan wrote: > >> I think it''s been around since Rails 2.1 >> >> Project.first >> Project.last >> Project.all >> >> all work. >> > > I think the first last and all refers to rows. I want to select columns. >If I''m not wrong, relational algebra doesn''t identify a sequence between the elements of a row, i.e., I don''t think it gives you the ability to select a column specifically. That said, you could probe for the schema and get the elements using that - something like how the dynamic scaffold used to work in earlier Rails. Cheers, Mohit. 12/26/2008 | 1:27 PM. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Zhao, I think the most straightforward way is using columns.map (Project.columns.map { |c| puts c.name }), just like the inspect method of ActiveRecord: inspect() Returns a string like ‘Post id:integer, title:string, body:text‘ # File vendor/rails/activerecord/lib/active_record/base.rb, line 1348 1348: def inspect 1349: if self == Base 1350: super 1351: elsif abstract_class? 1352: "#{super}(abstract)" 1353: elsif table_exists? 1354: attr_list = columns.map { |c| "#{c.name}: #{c.type}" } * '', '' 1355: "#{super}(#{attr_list})" 1356: else 1357: "#{super}(Table doesn''t exist)" 1358: end 1359: end Cheers, Sazima On Dec 26, 3:27 am, Mohit Sindhwani <mo_m...-RxrYI66vbj0AvxtiuMwx3w@public.gmane.org> wrote:> Zhao Yi wrote: > > Ryan wrote: > > >> I think it''s been around since Rails 2.1 > > >> Project.first > >> Project.last > >> Project.all > > >> all work. > > > I think the first last and all refers to rows. I want to select columns. > > If I''m not wrong, relational algebra doesn''t identify a sequence between > the elements of a row, i.e., I don''t think it gives you the ability to > select a column specifically. That said, you could probe for the schema > and get the elements using that - something like how the dynamic > scaffold used to work in earlier Rails. > > Cheers, > Mohit. > 12/26/2008 | 1:27 PM.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On another side note: @Ryan - You wrote: A more efficient way is to do this is: @project_names = Project.all(:select => ''name'').map(&:name) While you''ve made a database optimisation there, you''ve also used the shorthand notation for the map function. I read somewhere that map (&:name) is quite a bit less efficient than map{ |p| p.name } unfortunately I can''t find the article that convinced me in the first place... I can only find this blog with the issue mentioned in the comments: http://blog.hasmanythrough.com/2006/3/7/symbol-to-proc-shorthand On Dec 27, 2:23 am, Sazima <rsaz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Zhao, > > I think the most straightforward way is using columns.map > (Project.columns.map { |c| puts c.name }), just like the inspect > method of ActiveRecord: > > inspect() > > Returns a string like ‘Post id:integer, title:string, body:text‘ > > # File vendor/rails/activerecord/lib/active_record/base.rb, line 1348 > 1348: def inspect > 1349: if self == Base > 1350: super > 1351: elsif abstract_class? > 1352: "#{super}(abstract)" > 1353: elsif table_exists? > 1354: attr_list = columns.map { |c| "#{c.name}: #{c.type}" } > * '', '' > 1355: "#{super}(#{attr_list})" > 1356: else > 1357: "#{super}(Table doesn''t exist)" > 1358: end > 1359: end > > Cheers, Sazima > > On Dec 26, 3:27 am, Mohit Sindhwani <mo_m...-RxrYI66vbj0AvxtiuMwx3w@public.gmane.org> wrote: > > > Zhao Yi wrote: > > > Ryan wrote: > > > >> I think it''s been around since Rails 2.1 > > > >> Project.first > > >> Project.last > > >> Project.all > > > >> all work. > > > > I think the first last and all refers to rows. I want to select columns. > > > If I''m not wrong, relational algebra doesn''t identify a sequence between > > the elements of a row, i.e., I don''t think it gives you the ability to > > select a column specifically. That said, you could probe for the schema > > and get the elements using that - something like how the dynamic > > scaffold used to work in earlier Rails. > > > Cheers, > > Mohit. > > 12/26/2008 | 1:27 PM.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeremy Weiskotten
2008-Dec-27 03:25 UTC
Re: How to get one columns as an array from database
Jonathan Fantham wrote:> On another side note: > > @Ryan - You wrote: > > A more efficient way is to do this is: > @project_names = Project.all(:select => ''name'').map(&:name) > > While you''ve made a database optimisation there, you''ve also used the > shorthand notation for the map function. I read somewhere that map > (&:name) is quite a bit less efficient than map{ |p| p.name } > > unfortunately I can''t find the article that convinced me in the first > place... I can only find this blog with the issue mentioned in the > comments: > > http://blog.hasmanythrough.com/2006/3/7/symbol-to-proc-shorthandThe Symbol#to_proc syntax is slower than a block, but unless you''re doing it thousands of times, the single trip to the database is probably at least a few orders of magnitude more expensive. -- 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 -~----------~----~----~----~------~----~------~--~---
Cool. Learn something new all the time. Thanks. On Dec 26, 5:38 pm, Jonzo <j.fant...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On another side note: > > @Ryan - You wrote: > > A more efficient way is to do this is: > @project_names = Project.all(:select => ''name'').map(&:name) > > While you''ve made a database optimisation there, you''ve also used the > shorthand notation for the map function. I read somewhere that map > (&:name) is quite a bit less efficient than map{ |p| p.name } > > unfortunately I can''t find the article that convinced me in the first > place... I can only find this blog with the issue mentioned in the > comments: > > http://blog.hasmanythrough.com/2006/3/7/symbol-to-proc-shorthand > > On Dec 27, 2:23 am, Sazima <rsaz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Zhao, > > > I think the most straightforward way is using columns.map > > (Project.columns.map { |c| puts c.name }), just like the inspect > > method of ActiveRecord: > > > inspect() > > > Returns a string like ‘Post id:integer, title:string, body:text‘ > > > # File vendor/rails/activerecord/lib/active_record/base.rb, line 1348 > > 1348: def inspect > > 1349: if self == Base > > 1350: super > > 1351: elsif abstract_class? > > 1352: "#{super}(abstract)" > > 1353: elsif table_exists? > > 1354: attr_list = columns.map { |c| "#{c.name}: #{c.type}" } > > * '', '' > > 1355: "#{super}(#{attr_list})" > > 1356: else > > 1357: "#{super}(Table doesn''t exist)" > > 1358: end > > 1359: end > > > Cheers, Sazima > > > On Dec 26, 3:27 am, Mohit Sindhwani <mo_m...-RxrYI66vbj0AvxtiuMwx3w@public.gmane.org> wrote: > > > > Zhao Yi wrote: > > > > Ryan wrote: > > > > >> I think it''s been around since Rails 2.1 > > > > >> Project.first > > > >> Project.last > > > >> Project.all > > > > >> all work. > > > > > I think the first last and all refers to rows. I want to select columns. > > > > If I''m not wrong, relational algebra doesn''t identify a sequence between > > > the elements of a row, i.e., I don''t think it gives you the ability to > > > select a column specifically. That said, you could probe for the schema > > > and get the elements using that - something like how the dynamic > > > scaffold used to work in earlier Rails. > > > > Cheers, > > > Mohit. > > > 12/26/2008 | 1:27 PM.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---