Reputation: 2102
In our project we have a user who can have many credit cards. He should only have one default (boolean: true) at a time. From the current UI and the current application logic, it is impossible to have more than one. But if you open a rails console you can change and have more than one.
Is it a good practice to write some extra code, which it will check all the credit card records of a user, restricting the number of default credit cards to one on the model level? Or is this over-engineering?
Upvotes: 0
Views: 53
Reputation: 1023
On my opinion, writing additional validation is a good practice. Models is a place for code related to persistent business objects (rely on Rails conventions). If you business object user
has a collection of credit cards
and this collection has some business rules, it should be described in a model.
Some arguments:
Upvotes: 1
Reputation: 880
I would prohibit it in the model, especially if it would break if there were two defaults. That way a future programmer will know that it's bad and, if he doesn't notice and does it anyway, he will be informed.
Upvotes: 2