I have a “Chicken or the Egg” problem here…
I have a model with a unique field that is created with the record id (the id of the model) and a string, I have a :null=>false in my migration for this field (the hash) so… This hash should be generated automatically when saving the object with for example a before_validation but the problem is that I need the id from the database to create the hash, but I don’t have this id until the object is saved… so How do I solve this? It’s not an option to change the database restriction because this is already in production, and also is not an option to change the hash seeds, I HAVE to use ID…
What I’m doing right now (I know is not a nice solution) is to save a dummy in the hash field and save the record, after this I create the hash and save the object again… but this is made in the controller and I want to have all the business logic in the model…
Any Idea?
Use an
after_createin the model:This will save the record twice, just like you’re doing in the controller, except it will do it inside the model.