Greetings! I''m trying to set up a validation to ensure that a record is unique across three columns; col_one, col_two, col_three. tried this: validates_uniqueness_of :col_one, :scope=>[:col_two, :col_three] and it really doesn''t work at all like I had hoped. the docs says this should work class TeacherSchedule < ActiveRecord::Base validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id] end Just want to know if there are any experiences out there to share before I write my own validation code. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tom Allison <tom@...> writes:> > > Greetings!I''m trying to set up a validation to ensure that a record isunique across three columns; col_one, col_two, col_three. tried this:validates_uniqueness_of :col_one, :scope=>[:col_two, :col_three]and it really doesn''t work at all like I had hoped.the docs says this should work class TeacherSchedule < ActiveRecord::Base> validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id] > end > Just want to know if there are any experiences out there to share before Iwrite my own validation code.>Composite keys are frowned upon. You might want to reconsider what you are attempting and introduce a few models to handle the relationship. Of course, you could always create a customer validator. See the rails API. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Yeah, but in this case I''m just regurgitation someone else''s data and I don''t want to pick up duplicate records. On Sun, Jun 19, 2011 at 7:11 PM, Andrew Skegg <andrewskegg-BUHhN+a2lJ4@public.gmane.org> wrote:> Tom Allison <tom@...> writes: > > > > > > > Greetings!I''m trying to set up a validation to ensure that a record is > unique across three columns; col_one, col_two, col_three. tried > this:validates_uniqueness_of :col_one, :scope=>[:col_two, :col_three]and it > really doesn''t work at all like I had hoped.the docs says this should work > class TeacherSchedule < ActiveRecord::Base > > validates_uniqueness_of :teacher_id, :scope => [:semester_id, > :class_id] > > end > > Just want to know if there are any experiences out there to share before > I > write my own validation code. > > > > > Composite keys are frowned upon. You might want to reconsider what you are > attempting and introduce a few models to handle the relationship. > > Of course, you could always create a customer validator. See the rails > API. > > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Can you post your sample data here and if possible your model and controller code? I have used it on several apps, and works just fine for me. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/w6ZPt_0LCMAJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.