Here is the schema that I am working with: courses -> courses_teachers <- teachers I want to ensure that a teacher is not assigned the same course more than once. In other words, if I''m in the courses_teachers table as teaching Spanish 101, if someone tries to add that same teacher/course combination to the table again, it will not be allowed. I thought I could use something like validates_uniqueness_of ( http://rails.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000625 ), but I''m not sure how to go about. How do you do this? Thanks, kd
On Apr 20, 2005, at 5:09 PM, Keith Donaldson wrote:> Here is the schema that I am working with: > > courses -> courses_teachers <- teachers > > I want to ensure that a teacher is not assigned the same course more > than once.Although there may be a more rails-happy solution, if courses_teachers is a table with two-columns only, with the combination used as the primary key, it will not be possible to insert a duplicate row.
Thanks ... I''m using SQLite3 and maybe table now looks like this:
courses_teachers (
course_id integer,
teacher_id integer,
primary key(course_id,teacher_id));
When I try to create a duplicate I get this error message:
ActiveRecord::StatementInvalid in Teacher#add
columns course_id, teacher_id are not unique: INSERT INTO
courses_teachers (''teacher_id'', ''course_id'')
VALUES (''1'', ''1'')
What''s the proper (user friendly) way to handle this exception?
On 4/21/05, Gavin Kistner <gavin-XtLdkLkwz3ZWk0Htik3J/w@public.gmane.org>
wrote:> On Apr 20, 2005, at 5:09 PM, Keith Donaldson wrote:
> > Here is the schema that I am working with:
> >
> > courses -> courses_teachers <- teachers
> >
> > I want to ensure that a teacher is not assigned the same course more
> > than once.
>
> Although there may be a more rails-happy solution, if courses_teachers
> is a table with two-columns only, with the combination used as the
> primary key, it will not be possible to insert a duplicate row.
>
>