Sijo Kg
2008-Sep-10 06:24 UTC
Using the collect method to collect to a 2 dimensional array
Hi I am trying to collect name and email of a person to an array (Or hash?) like [[name1,emailid],[name2,emailid].......] But I dont know how to do that. I tried like email_array Contact.get_email_id_and_name_of_contacts(problem.reported_by_id) unless problem.reported_by_id.blank? And in Contact class def self.get_email_ids_of_contacts(contact_id) return Contact.find_by_id(contact_id).name, Contact.find_by_id(contact_id).contact_email_addresses.find(:all, :conditions => [''contact_email_address_type_id=?'',2]).collect { |mail| mail.email} end Here I tried to return two value at a time .But not suceeded .How can I do that?Please help Sijo -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-10 06:54 UTC
Re: Using the collect method to collect to a 2 dimensional array
On 10 Sep 2008, at 07:24, Sijo Kg wrote:> > Hi > I am trying to collect name and email of a person to an array (Or > hash?) like > [[name1,emailid],[name2,emailid].......] But I dont know how to do > that. > I tried like > > email_array > Contact.get_email_id_and_name_of_contacts(problem.reported_by_id) > unless > problem.reported_by_id.blank? > > And in Contact class > > def self.get_email_ids_of_contacts(contact_id) > return Contact.find_by_id(contact_id).name, > Contact.find_by_id(contact_id).contact_email_addresses.find(:all, > :conditions => [''contact_email_address_type_id=?'',2]).collect { |mail| > mail.email} > end >Collect just bungs whatever your block evaluates to in an array. If that block evaluates to an array then you''ll get an array of arrays back. Fred> Here I tried to return two value at a time .But not suceeded .How > can I do that?Please help > > Sijo > -- > 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 -~----------~----~----~----~------~----~------~--~---
Ilan Berci
2008-Sep-10 16:20 UTC
Re: Using the collect method to collect to a 2 dimensional array
Sijo, not sure, but looking at your code it seems you may want to do something like the following def self.get_email_ids_of_contacts(contact_id) contact = Contact.find_by_id(contact_id) contact.contact_email_addresses.find(:all, :conditions => []).map do |mail| [contact.name, mail.email] end end Sijo Kg wrote:> > def self.get_email_ids_of_contacts(contact_id) > return Contact.find_by_id(contact_id).name, > Contact.find_by_id(contact_id).contact_email_addresses.find(:all, > :conditions => [''contact_email_address_type_id=?'',2]).collect { |mail| > mail.email} > end-- 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 -~----------~----~----~----~------~----~------~--~---
Sijo Kg
2008-Sep-11 05:28 UTC
Re: Using the collect method to collect to a 2 dimensional a
Hi Lots of thanks for your great reply.This is what i needed.Now it works. But I have one more problem. I have to find group managers name and email like above and I tried like below name_and_email_managers=Group.get_managers_name_and_emails(problem.primary_assignee_group_id) unless problem.primary_assignee_group_id.blank? And now in Group class def self.get_managers_name_and_emails(grp_id) managers=Group.find_by_id(grp_id).contacts.find(:all,:conditions => [''group_user_type_id = ?'',4]) managers.each do |manager| manager.contact_email_addresses.find(:all, :conditions => [''contact_email_address_type_id=?'',2]).map do |mail| [manager.name,mail.email] end end end But this does not give array like above?Could you please tell me where is the fault?A group may have more than one manager and each manager has more than one email addresses of the above type.That is why I wrote code like above Groups and Contacts are connected through user groups (has_many :through relation) Sijo -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-11 08:06 UTC
Re: Using the collect method to collect to a 2 dimensional a
On Sep 11, 6:28 am, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> And now in Group class > def self.get_managers_name_and_emails(grp_id) > managers=Group.find_by_id(grp_id).contacts.find(:all,:conditions => > [''group_user_type_id = ?'',4]) > managers.each do |manager| > manager.contact_email_addresses.find(:all, :conditions => > [''contact_email_address_type_id=?'',2]).map do |mail| > [manager.name,mail.email] > end > end > end > But this does not give array like above?Could you please tell me > where is the fault?A group may have more than one manager and each > manager has more than one email addresses of the above type.That is why > I wrote code like above Groups and Contacts are connected through user > groups (has_many :through relation) >You''re generating these arrays using map but then you''re just throwing them away (each returns the collection it iterated over. Perhaps you mean that to be map or inject instead to concatenate the various arrays produced? Fred> Sijo > -- > 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 -~----------~----~----~----~------~----~------~--~---
Igor K.
2008-Sep-11 15:17 UTC
Re: Using the collect method to collect to a 2 dimensional a
Frederick Cheung wrote:> On Sep 11, 6:28�am, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> end >> � � � � But this does not give array like above?Could you please tell me >> where is the fault?A group may have more than one manager and each >> manager has more than one email addresses of the above type.That is why >> I wrote code like above �Groups and Contacts are connected through user >> groups (has_many :through relation) >> > You''re generating these arrays using map but then you''re just throwing > them away (each returns the collection it iterated over. Perhaps you > mean that to be map or inject instead to concatenate the various > arrays produced? > > FredArray.partial -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---