views:

45

answers:

1

Hey Guys,

Lets say the model for books is:

created_at
user_id (int)
author_id (int)
genre_id (int)
pages (int)

I want to let people change the "created_at" field upon record creation. I also want to make sure that a record can't have the exact same created_at, user_id, and author_id as any other record (i.e. each needs its own date).

Whats the best to perform this type of validation, and how should I go about doing it?

Best,

Elliot

+1  A: 
validates_uniqueness_of :user_id, :scope => [:author_id, :created_at]
Salil
I would also add a separate column for the date you want to change rather than using Rails' `created_at` column. You don't lose anything this way but you possibly avoid a large number of headaches.
Matt S
does this validate the unqiueness of the created_at as well though?And also, looking back I agree about the date, but I already have over 500 records using the created_at :\
Elliot
you can write a migration to duplicate the data of created_at(500 columns) to new_created_at.
Vamsi
to answer my sub-question: validates_uniqueness_of :user_id, :scope => [:author_id, :created_at]
Elliot
EDITED Thanx @Eliot.
Salil