John Small
2008-Sep-09 16:41 UTC
a simple way to generate a comma separated list of codes?
I have a model, Account, with many Courses. I want to display the course codes for those courses as comma delimited text in an attribute of the account. So I would have Account << ActiveRecord::Base has_many :courses def get_course_codes_as_text courses.each {|course| something something but I don''t know what end Now I could do it the hard way by setting up a string var and pumping stuff into it inside the block. But there''s an easier Rubyesque way to do this which I''ve seen in code elsewhere but I can''t find the code now so I thought I''d ask here. Something to do with join or map or something John Small -- 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 -~----------~----~----~----~------~----~------~--~---
Shiv N Gautam
2008-Sep-09 16:46 UTC
Re: a simple way to generate a comma separated list of codes?
On Tue, Sep 9, 2008 at 10:11 PM, John Small < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I have a model, Account, with many Courses. I want to display the course > codes for those courses as comma delimited text in an attribute of the > account. So I would have > > Account << ActiveRecord::Base > has_many :courses > > def get_course_codes_as_text > courses.each {|course| something something but I don''t know what > end > > Now I could do it the hard way by setting up a string var and pumping > stuff into it inside the block. But there''s an easier Rubyesque way to > do this which I''ve seen in code elsewhere but I can''t find the code now > so I thought I''d ask here. Something to do with join or map or something > > John Small > -- > Posted via http://www.ruby-forum.com/. > > > >a = [] courses.each { |course| a.push(course.code) } a = a.join "," This ought to do it. ~~ Shiv --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Vince Hodges
2008-Sep-09 16:50 UTC
Re: a simple way to generate a comma separated list of codes?
On Tue, Sep 9, 2008 at 9:46 AM, Shiv N Gautam <s.n.gautam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Tue, Sep 9, 2008 at 10:11 PM, John Small < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> >> I have a model, Account, with many Courses. I want to display the course >> codes for those courses as comma delimited text in an attribute of the >> account. So I would have >> >> Account << ActiveRecord::Base >> has_many :courses >> >> def get_course_codes_as_text >> courses.each {|course| something something but I don''t know what >> end >> >> Now I could do it the hard way by setting up a string var and pumping >> stuff into it inside the block. But there''s an easier Rubyesque way to >> do this which I''ve seen in code elsewhere but I can''t find the code now >> so I thought I''d ask here. Something to do with join or map or something >> >> John Small >> -- >> Posted via http://www.ruby-forum.com/. >> >> >> > a = [] > courses.each { |course| > a.push(course.code) > } > a = a.join "," > > > This ought to do it. > > > ~~ > Shiv > > > >or.. (a bit more concise) courses.collect(&:code).join('','') -- Vince Hodges http://www.imbas.ca/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
William Yeung
2008-Sep-09 16:55 UTC
Re: a simple way to generate a comma separated list of codes
See if I qualify: courses.collect {|c| c.code}.join(",") John Small wrote:> I have a model, Account, with many Courses. I want to display the course > codes for those courses as comma delimited text in an attribute of the > account. So I would have > > Account << ActiveRecord::Base > has_many :courses > > def get_course_codes_as_text > courses.each {|course| something something but I don''t know what > end > > Now I could do it the hard way by setting up a string var and pumping > stuff into it inside the block. But there''s an easier Rubyesque way to > do this which I''ve seen in code elsewhere but I can''t find the code now > so I thought I''d ask here. Something to do with join or map or something > > John Small-- 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 -~----------~----~----~----~------~----~------~--~---
Bosco So
2008-Sep-09 18:21 UTC
Re: a simple way to generate a comma separated list of codes
Newbie question: how do I prevent your code snippet from becoming an n- fetch? Eager loading will work, but if all I want are the codes, then eager loading of the associated courses will over-retrieve data. Yes, yes, I know - don''t over optimize. But if I know how to optimize, at least I can put a comment in the code to remind me when I get twitter-size traffic. :-) -- Bosco On Sep 9, 9:55 am, William Yeung <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> See if I qualify: > > courses.collect {|c| c.code}.join(",") > > > > John Small wrote: > > I have a model, Account, with many Courses. I want to display the course > > codes for those courses as comma delimited text in an attribute of the > > account. So I would have > > > Account << ActiveRecord::Base > > has_many :courses > > > def get_course_codes_as_text > > courses.each {|course| something something but I don''t know what > > end > > > Now I could do it the hard way by setting up a string var and pumping > > stuff into it inside the block. But there''s an easier Rubyesque way to > > do this which I''ve seen in code elsewhere but I can''t find the code now > > so I thought I''d ask here. Something to do with join or map or something--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
John Small
2008-Sep-09 18:48 UTC
Re: a simple way to generate a comma separated list of codes
William Yeung wrote:> See if I qualify: > > courses.collect {|c| c.code}.join(",") >Yes, that''s the code snippet I was looking for! Thanks John Small -- 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 -~----------~----~----~----~------~----~------~--~---