Gearóid O'Ceallaigh
2008-Dec-01 16:07 UTC
Checking if an association already exists between two records
Hey, I''m creating an app which has two models - bands and users. A relationship between users and bands in many to many. However, when I''m adding an existing to user to a band (ie. updating the bands_users table) how can i check if the relationship between the user and the band already exists? For example, if a user with ID of 3 was already a member of a band with ID 5, and then somebody tried to make this connection again - I want to output an error message. Is there some special validation I can place in a bands_users.rb that validated the uniqueness of the entire record? Thanks, -ger --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten Müller
2008-Dec-01 16:37 UTC
Re: Checking if an association already exists between two records
You can use the scopt attribute of validates_uniqueness_of But this would require, that the intermediate table between users/bands has it''s own model in this model (BandsUsers) validates_uniqueness_of :user_id, :scope => band_id --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Brandon Keepers
2008-Dec-01 17:29 UTC
Re: Checking if an association already exists between two records
On Mon, Dec 1, 2008 at 11:07 AM, Gearóid O''Ceallaigh <gearoid.k-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hey, > > I''m creating an app which has two models - bands and users. A > relationship between users and bands in many to many. > > However, when I''m adding an existing to user to a band (ie. updating > the bands_users table) how can i check if the relationship between the > user and the band already exists? For example, if a user with ID of 3 > was already a member of a band with ID 5, and then somebody tried to > make this connection again - I want to output an error message. Is > there some special validation I can place in a bands_users.rb that > validated the uniqueness of the entire record?There is a :uniq option that will ignore duplicates: class Band < ActiveRecord::Base has_and_belongs_to_many :users, :uniq => true end This won''t raise an error, but it will prevent duplicate associations. Hope that Helps Brandon -- -------------------------------------------------------------------------------- Training by Collective Idea: Ruby on Rails training in a vacation setting http://training.collectiveidea.com — San Antonio, TX — Jan 20-23 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
pepe
2008-Dec-03 03:24 UTC
Re: Checking if an association already exists between two records
I guess you have your reasons but why not ignore the fact that the relationship already existed? Does it really matter to let the user know? Pepe On Dec 1, 12:29 pm, "Brandon Keepers" <bran...-NYlJC1mBhkRg9hUCZPvPmw@public.gmane.org> wrote:> On Mon, Dec 1, 2008 at 11:07 AM, Gearóid O''Ceallaigh > > <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hey, > > > I''m creating an app which has two models - bands and users. A > > relationship between users and bands in many to many. > > > However, when I''m adding an existing to user to a band (ie. updating > > the bands_users table) how can i check if the relationship between the > > user and the band already exists? For example, if a user with ID of 3 > > was already a member of a band with ID 5, and then somebody tried to > > make this connection again - I want to output an error message. Is > > there some special validation I can place in a bands_users.rb that > > validated the uniqueness of the entire record? > > There is a :uniq option that will ignore duplicates: > > class Band < ActiveRecord::Base > has_and_belongs_to_many :users, :uniq => true > end > > This won''t raise an error, but it will prevent duplicate associations. > Hope that Helps > > Brandon > -- > -------------------------------------------------------------------------------- > Training by Collective Idea: Ruby on Rails training in a vacation settinghttp://training.collectiveidea.com— San Antonio, TX — Jan 20-23--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---