Good evening all, I''m looking to do something similar to the following (pseudo code) - and I''m sure there''s got to be a way to do this in rails w/o writing some verbose logic: tag = Tag.new(''foo'') if !article.tags.contains(tag) then article.tags << tag article.save end It''s the *contains* piece I''m after. For some reason, I don''t relaly want to write if !article.tags.find(tag.id) then...but that might be my only bet. -- 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 -~----------~----~----~----~------~----~------~--~---
Once you have your tag object... article.tags << tag unless article.tags.include?(tag) But - I''m not sure if the object-object comparison that include? performs is what you''re really after. c. Cory wrote:> Good evening all, > > I''m looking to do something similar to the following (pseudo code) - and > I''m sure there''s got to be a way to do this in rails w/o writing some > verbose logic: > > tag = Tag.new(''foo'') > > if !article.tags.contains(tag) then > article.tags << tag > article.save > end > > It''s the *contains* piece I''m after. For some reason, I don''t relaly > want to write if !article.tags.find(tag.id) then...but that might be my > only bet.-- 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 -~----------~----~----~----~------~----~------~--~---
You can use include? to check if an item already exists in an array: a = [1,2,3] a.include?(3) # => true -Jonathan. On 11/6/06, Cory <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Good evening all, > > I''m looking to do something similar to the following (pseudo code) - and > I''m sure there''s got to be a way to do this in rails w/o writing some > verbose logic: > > tag = Tag.new(''foo'') > > if !article.tags.contains(tag) then > article.tags << tag > article.save > end > > It''s the *contains* piece I''m after. For some reason, I don''t relaly > want to write if !article.tags.find(tag.id) then...but that might be my > only bet. > > -- > 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 -~----------~----~----~----~------~----~------~--~---