Hi All, Newbie here working with an existing Rails project. I''m trying to create a method that will return a simple string of object values. I have an app that has companies that each are assigned to one or more branches. Then I have a contact who is assigned to a company. I''ve created an association with belongs_to for both the company and branch, and the branch and company has their own class in AR. So def get_company_branches my_branches = self.company.branches my_list = my_branches.each { |x| puts x.branch_name } return my_list end For some reason this returns an object id (the raw ruby object id I think) not the string i am looking for. Any suggestions? Best, Peter -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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 Feb 24, 5:07 pm, Peter D Bethke <peter.bet...-ee4meeAH724@public.gmane.org> wrote:> Hi All, > > Newbie here working with an existing Rails project. I''m trying to create a method that will return a simple string of object values. > > I have an app that has companies that each are assigned to one or more branches. Then I have a contact who is assigned to a company. I''ve created an association with belongs_to for both the company and branch, and the branch and company has their own class in AR. > > So > > def get_company_branches > > my_branches = self.company.branches > my_list = my_branches.each { |x| puts x.branch_name } > return my_list > end > > For some reason this returns an object id (the raw ruby object id I think) not the string i am looking for. Any suggestions? >puts just dumps things to stdout, so you definitely don''t want to be using that here. If you want to create an array based on the value of each object in a collection then you should use collect (also available as map). It creates a new array containing the result of evaluating the block for each object of the original collection. Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Thanks Fred! Does this look right? I''m still getting an error for some reason: def get_company_branches my_branches = self.company.branches my_list = my_branches.branch_name.collect! { |x| x + "," } return my_list end looking for it to output a concatenated string - still erroring out tho. :( Peter On Feb 24, 2011, at 1:01 PM, Frederick Cheung wrote:> > > On Feb 24, 5:07 pm, Peter D Bethke <peter.bet...-ee4meeAH724@public.gmane.org> wrote: >> Hi All, >> >> Newbie here working with an existing Rails project. I''m trying to create a method that will return a simple string of object values. >> >> I have an app that has companies that each are assigned to one or more branches. Then I have a contact who is assigned to a company. I''ve created an association with belongs_to for both the company and branch, and the branch and company has their own class in AR. >> >> So >> >> def get_company_branches >> >> my_branches = self.company.branches >> my_list = my_branches.each { |x| puts x.branch_name } >> return my_list >> end >> >> For some reason this returns an object id (the raw ruby object id I think) not the string i am looking for. Any suggestions? >> > > puts just dumps things to stdout, so you definitely don''t want to be > using that here. > If you want to create an array based on the value of each object in a > collection then you should use collect (also available as map). It > creates a new array containing the result of evaluating the block for > each object of the original collection. > > Fred > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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 24 February 2011 17:07, Peter D Bethke <peter.bethke-ee4meeAH724@public.gmane.org> wrote:> def get_company_branches > my_branches = self.company.branches > my_list = my_branches.each { |x| puts x.branch_name } > return my_list > endYou might find that something like this is what you''re after: def get_company_branches self.company.branches.collect(&:branch_name).join(", ") if self.company && self.company.branches end Have a look at the methods available to Array and Enumerable in both the Rails and Ruby apis. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Michael Pavling
2011-Feb-24 18:24 UTC
Re: Re: Outputting a string from an array of objects
On 24 February 2011 18:12, Peter D Bethke <peter.bethke-ee4meeAH724@public.gmane.org> wrote:> Thanks Fred! > > Does this look right? I''m still getting an error for some reason:Always post the error... just saying "I''ve got one" doesn''t help people point out where you might be having problems (although in this case, it''s ruby syntax... check the API for the ".collect" method :-) def get_company_branches my_branches = self.company.branches # you don''t really need these intermediate variables... just use self.branches.collect.... my_list = my_branches.collect { |x| x.branch_name } return my_list.join(", ") # "collect" returns an array, so we can join the array into a string, or maybe use the "to_sentence" extension to prettify it end ...my previous method just does the same, a little more compact. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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 Feb 24, 6:12 pm, Peter D Bethke <peter.bet...-ee4meeAH724@public.gmane.org> wrote:> Thanks Fred! > > Does this look right? I''m still getting an error for some reason: > > def get_company_branches > > my_branches = self.company.branches > my_list = my_branches.branch_name.collect! { |x| x + "," } > return my_list > endYou need to call collect (you don''t want collect!) on the array, not on the branch_name for an individual branch Fred> > looking for it to output a concatenated string - still erroring out tho. :( > > Peter > > On Feb 24, 2011, at 1:01 PM, Frederick Cheung wrote: > > > > > > > On Feb 24, 5:07 pm, Peter D Bethke <peter.bet...-ee4meeAH724@public.gmane.org> wrote: > >> Hi All, > > >> Newbie here working with an existing Rails project. I''m trying to create a method that will return a simple string of object values. > > >> I have an app that has companies that each are assigned to one or more branches. Then I have a contact who is assigned to a company. I''ve created an association with belongs_to for both the company and branch, and the branch and company has their own class in AR. > > >> So > > >> def get_company_branches > > >> my_branches = self.company.branches > >> my_list = my_branches.each { |x| puts x.branch_name } > >> return my_list > >> end > > >> For some reason this returns an object id (the raw ruby object id I think) not the string i am looking for. Any suggestions? > > > puts just dumps things to stdout, so you definitely don''t want to be > > using that here. > > If you want to create an array based on the value of each object in a > > collection then you should use collect (also available as map). It > > creates a new array containing the result of evaluating the block for > > each object of the original collection. > > > Fred > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.