Hi, I have an array of contact ids and an array of group ids. I''m trying to loop through all the contacts and add them to the group if they aren''t already in the group. The relationship is as follows: class Contact < ActiveRecord::Base has_many :contact_groups has_many :groups, :through => :contact_groups end class ContactGroup < ActiveRecord::Base belongs_to :contact belongs_to :group end class Group < ActiveRecord::Base has_many :contact_groups has_many :contacts, :through => :contact_groups end Here''s what I''m currently doing which works. However, the code is clunky. It''s the has_many :through relationship that''s throwing me for a loop... literally: for contact in params[:contact] for group in params[:group] ContactGroup.find_or_create_by_contact_id_and_group_id(contact, group) end end Is there a better way? Thanks! -Dave --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Given your problem statement:>I''m trying to loop through all the contacts and add them to the group if they >aren''t already in the group.And assuming your models/associations as defined above, and: @group = Group.find(1) Then this should loop through all contacts and add to the group any that aren''t already in the group: Contact.find(:all).each {|c| @group.contacts << c unless @group.contacts.include?(c)} c. -- 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 -~----------~----~----~----~------~----~------~--~---
Hi Cayce, I tried your suggestion but I keep receiving an error that reads: undefined method `contacts'' for #<Array:0x246be0c> This is how I implemented your suggestion: @group = Group.find(params[:group]) Contact.find(params[:contact]).each {|c| @group.contacts << c unless @ group.contacts.include?(c)} params[:group] and params[:contact] hold an array of IDs that originate from a series of check boxes. I''m pretty sure the relationships are set up right, so I''m kind of lost as to why this undefined method error is occurring. Thanks, Dave On 11/9/06, Cayce Balara <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Given your problem statement: > > >I''m trying to loop through all the contacts and add them to the group if > they > >aren''t already in the group. > > And assuming your models/associations as defined above, and: > > @group = Group.find(1) > > Then this should loop through all contacts and add to the group any that > aren''t already in the group: > > Contact.find(:all).each {|c| @group.contacts << c unless > @group.contacts.include?(c)} > > c. > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Dave Hoefler wrote:> Hi Cayce, > > I tried your suggestion but I keep receiving an error that reads: > > undefined method `contacts'' for #<Array:0x246be0c> > > This is how I implemented your suggestion: > > @group = Group.find(params[:group]) > Contact.find(params[:contact]).each {|c| @group.contacts << c unless @ > group.contacts.include?(c)} > > params[:group] and params[:contact] hold an array of IDs that originate > from > a series of check boxes. > > I''m pretty sure the relationships are set up right, so I''m kind of lost > as > to why this undefined method error is occurring. > > Thanks, > DaveLooks like Group.find(params[:group]) is returning an Array rather than a single instance of Group. To use Cayce''s algorithm (nicely done, by the way), @group will need to represent a single instance of Group Cheers. -- 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 -~----------~----~----~----~------~----~------~--~---
> Looks like Group.find(params[:group]) is returning an Array rather than > a single instance of Group. To use Cayce''s algorithm (nicely done, by > the way), @group will need to represent a single instance of Group > > Cheers.Yep, that''s definitely the problem. Initially, I''d have said try this... Group.find(params[:group]).each do |g| Contact.find(:all).each {|c| g.contacts << c unless g.contacts.include?(c)} end Only - this is going through all contacts in the database and adding those that don''t exist in the group. I''m gathering from your second post that what you''re after is to add all contacts in params[:contact] to all groups in params[:group]. In that case, you just need to change the Contact.find() parms - this should work for you... Group.find(params[:group]).each do |g| Contact.find(params[:contact]).each {|c| g.contacts << c unless g.contacts.include?(c)} end Enjoy. c. -- 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Nov-10 04:06 UTC
Re: There''s got to be a better way!
Hi -- On Fri, 10 Nov 2006, Cayce Balara wrote:> >> Looks like Group.find(params[:group]) is returning an Array rather than >> a single instance of Group. To use Cayce''s algorithm (nicely done, by >> the way), @group will need to represent a single instance of Group >> >> Cheers. > > > > Yep, that''s definitely the problem. Initially, I''d have said try this... > > Group.find(params[:group]).each do |g| > Contact.find(:all).each {|c| g.contacts << c unless > g.contacts.include?(c)} > end > > Only - this is going through all contacts in the database and adding > those that don''t exist in the group. I''m gathering from your second post > that what you''re after is to add all contacts in params[:contact] to all > groups in params[:group]. In that case, you just need to change the > Contact.find() parms - this should work for you... > > Group.find(params[:group]).each do |g| > Contact.find(params[:contact]).each {|c| g.contacts << c unless > g.contacts.include?(c)} > endI think you can save a lot of database hits if you load the contacts eagerly: Group.find(params[:group], :include => "contacts") Otherwise every reference to g.contacts is going to have to go back to the database. David -- David A. Black | dblack-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.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 -~----------~----~----~----~------~----~------~--~---
Cayce, That worked like a charm! Learning Rails before really learning Ruby makes some things harder than what they really should be. David, I think it''s a good idea to eagerly load the contacts like you suggested. Thanks to all that helped! -Dave On 11/9/06, dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org <dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org> wrote:> > > Hi -- > > On Fri, 10 Nov 2006, Cayce Balara wrote: > > > > >> Looks like Group.find(params[:group]) is returning an Array rather than > >> a single instance of Group. To use Cayce''s algorithm (nicely done, by > >> the way), @group will need to represent a single instance of Group > >> > >> Cheers. > > > > > > > > Yep, that''s definitely the problem. Initially, I''d have said try this... > > > > Group.find(params[:group]).each do |g| > > Contact.find(:all).each {|c| g.contacts << c unless > > g.contacts.include?(c)} > > end > > > > Only - this is going through all contacts in the database and adding > > those that don''t exist in the group. I''m gathering from your second post > > that what you''re after is to add all contacts in params[:contact] to all > > groups in params[:group]. In that case, you just need to change the > > Contact.find() parms - this should work for you... > > > > Group.find(params[:group]).each do |g| > > Contact.find(params[:contact]).each {|c| g.contacts << c unless > > g.contacts.include?(c)} > > end > > I think you can save a lot of database hits if you load the contacts > eagerly: > > Group.find(params[:group], :include => "contacts") > > Otherwise every reference to g.contacts is going to have to go back to > the database. > > > David > > -- > David A. Black | dblack-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org > Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] > DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] > [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com > [2] http://dablog.rubypal.com | [4] http://www.rubycentral.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 -~----------~----~----~----~------~----~------~--~---
> > I think you can save a lot of database hits if you load the contacts > eagerly: > > Group.find(params[:group], :include => "contacts") > > Otherwise every reference to g.contacts is going to have to go back to > the database. > > > David > > -- > David A. Black | dblack-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org > Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] > DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] > [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com > [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org+1 on that. c. -- 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Nov-10 19:33 UTC
Re: There''s got to be a better way!
Hi -- On Fri, 10 Nov 2006, Dave Hoefler wrote:> Cayce, > > That worked like a charm! Learning Rails before really learning Ruby makes > some things harder than what they really should be.That''s why you should pick up "Ruby for Rails" :-) (No, really! It''s true! Self-serving, but true :-) David -- David A. Black | dblack-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.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 -~----------~----~----~----~------~----~------~--~---
I actually do own that book (and I thank you for writing it!). There''s so much info in there. Takes awhile to get through it :-) On 11/10/06, dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org <dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org> wrote:> > > Hi -- > > On Fri, 10 Nov 2006, Dave Hoefler wrote: > > > Cayce, > > > > That worked like a charm! Learning Rails before really learning Ruby > makes > > some things harder than what they really should be. > > That''s why you should pick up "Ruby for Rails" :-) (No, really! It''s > true! Self-serving, but true :-) > > > David > > -- > David A. Black | dblack-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org > Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] > DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] > [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com > [2] http://dablog.rubypal.com | [4] http://www.rubycentral.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 -~----------~----~----~----~------~----~------~--~---
Dave Hoefler wrote:> I actually do own that book (and I thank you for writing it!). There''s > so > much info in there. Takes awhile to get through it :-)Well, this is not at all self serving. Let me just say that being on my second time through David''s book (amazing the things that you pick up on once you have a bit of undertanding under your belt.) and having attended a week long bootcamp that he taught, the book really should be required reading. -- 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 -~----------~----~----~----~------~----~------~--~---