1

I have books table and isbn field in it. When the user update existing book, if he changes the isbn code and save, isbn should be checked to unique, otherwise unique validation must be skipped.

There's unique validation but it is checking even isbn is not changed on update.

I have one idea. One variable will be added to Book model to keep old value of isbn. When the record is updated, old value and new value will be compared. If it is changed, custom validation will run.

Is it optimal way or is there any better way to do this?

Sarvar Nishonboyev
  • 12,262
  • 10
  • 69
  • 70

1 Answers1

0

It works like this, you are setting:

['a1', 'unique']

and thanks to that you can`t have duplicate values in your table.

If u want to skip validation check of your isbn [ when the user updates, i don`t know why u would do that?? You want to have two books of same isbn? ], make SCENARIO with some:

In update proecess if[book isbn is changed] 
set scenario 1;
In update process if[book isbn isn`t changed]
set scenario 2;

in create process use deafult scenario
fsn
  • 549
  • 3
  • 11
  • no, it should be checked if isbn changed. I don't think it can be solved using scenario – Sarvar Nishonboyev Oct 10 '15 at 10:09
  • I want skip if the user didn't change current ISBN, coz current ISBN is exist in this book, if I don't skip it, it's giving an error that "This ISBN is already exist". of course it exists, coz this book has this ISBN. Unique validation works only the ISBN is changed to other number. hope It made a sense. thank you for ur suggestion. it seems good. – Sarvar Nishonboyev Oct 10 '15 at 11:25