mercredi 2 septembre 2015

How to make a hook that will update an active record model before it is read by MySql?

The problem is this:

If you send an update query to mysql, it will not insert anything if it thinks the values are the same (for the purposes of speed).

I have a lot of records that have data I am now considering not valid, and I want to add a before_save hook to make sure data imported subsequently will be fixed (for example, add http:// to urls that don't have it), but it will only trigger if the record is new or if the url is changed.

I believe this is happening because SQL is reading and comparing the record with the active record model instance I'm trying to save, and deciding they are the same before my before_save code to add 'http://' is triggered.

I dislike hacking this before_save method into the controller, as the model is saved from a few different places and by some scripts. But they all use the active record model.

Is there a before_read hook or something that could solve this problem?

Aucun commentaire:

Enregistrer un commentaire