I have this table associating airlines with rental cars. It has four columns - user_id, airline_id, rentalcar_id and id. I need to make sure that the combo of airline_id and rentalcar_id is always unique - in other words, a user cannot have two fields with the same airline_id and rentalcar_id. Is this done with one of the validation commands in the model? I have look at the one that I can find and none seems to fit all that well. Thanks, -S -- 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 could try: validates_uniqueness_of :rentalcar_id, :scope => :airline_id Whichever is the widest scope (e.g., can an airline have a lot of rental cars? Or the other way around?) should probably go with :scope. I don''t _think_ it would affect anything, but for modeling''s sake it should be that way. :) --Jeremy On 10/11/07, Shandy Nantz <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I have this table associating airlines with rental cars. It has four > columns - user_id, airline_id, rentalcar_id and id. I need to make sure > that the combo of airline_id and rentalcar_id is always unique - in > other words, a user cannot have two fields with the same airline_id and > rentalcar_id. Is this done with one of the validation commands in the > model? I have look at the one that I can find and none seems to fit all > that well. Thanks, > > -S > -- > Posted via http://www.ruby-forum.com/. > > > >-- http://www.jeremymcanally.com/ My books: Ruby in Practice http://www.manning.com/mcanally/ My free Ruby e-book http://www.humblelittlerubybook.com/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
> I have this table associating airlines with rental cars. It has four > columns - user_id, airline_id, rentalcar_id and id. I need to make sure > that the combo of airline_id and rentalcar_id is always unique - in > other words, a user cannot have two fields with the same airline_id and > rentalcar_id. Is this done with one of the validation commands in the > model? I have look at the one that I can find and none seems to fit all > that well. Thanks,Create a unique index on user_id, airline_id, rentalcar_id in your migration... that would be one way --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
randomutterings-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-12 14:21 UTC
Re: unique entries
I second that. I''m using validates_uniquemess_of with :scope for the same purpose in one of my projects. In the testing I have done it did not matter which one went in the :scope. I agree that the widest would be the better choice for readability later. On Oct 11, 5:36 pm, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You could try: > > validates_uniqueness_of :rentalcar_id, :scope => :airline_id > > Whichever is the widest scope (e.g., can an airline have a lot of > rental cars? Or the other way around?) should probably go with > :scope. I don''t _think_ it would affect anything, but for modeling''s > sake it should be that way. :) > > --Jeremy > > On 10/11/07, Shandy Nantz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > I have this table associating airlines with rental cars. It has four > > columns - user_id, airline_id, rentalcar_id and id. I need to make sure > > that the combo of airline_id and rentalcar_id is always unique - in > > other words, a user cannot have two fields with the same airline_id and > > rentalcar_id. Is this done with one of the validation commands in the > > model? I have look at the one that I can find and none seems to fit all > > that well. Thanks, > > > -S > > -- > > Posted viahttp://www.ruby-forum.com/. > > --http://www.jeremymcanally.com/ > > My books: > Ruby in Practicehttp://www.manning.com/mcanally/ > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
randomutterings-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I second that. I''m using validates_uniquemess_of with :scope for the > same purpose in one of my projects. In the testing I have done it did > not matter which one went in the :scope. I agree that the widest > would be the better choice for readability later. > > On Oct 11, 5:36 pm, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>What I was trying to do was to have a user with an airline membership be associated to as many rental car companies as they desired, but to insure the integrity of the database, I didn''t want an entry that had all three columns the same - user_id, airline_id, and rentalcar_id. The same user_id and airline_id was ok as long as they all had different rentalcar_id''s. Hope this makes some sense, because it was confusing the hell out of me. Anyway, here is what I did: validates_uniqueness_of :rentalcar_id, :scope => :airline_id and :_user_id and it seems to work. I didn''t know that I could say and but apperently I can. Thanks all, -S -- 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 -~----------~----~----~----~------~----~------~--~---
On 10/12/07, Shandy Nantz <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> validates_uniqueness_of :rentalcar_id, :scope => :airline_id and > :_user_id > > and it seems to work. I didn''t know that I could say and but apperently > I can. Thanks all,Well, you can "say" it, but Ruby will parse that as: validates_uniqueness_of(:rentalcar_id, :scope => :airline_id) and :_user_id The "and" part is a no-op, which I''m guessing isn''t what you intend. If you want to use two columns for the scope, the syntax is: :scope => [:airline_id, :user_id] which is illustrated in the API docs for validates_uniqueness_of --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---