mercredi 22 août 2018

Where to put create model and association logic in rails?

I have a model A with some association to B via a join table AB. In the create action for the A controller I create the join model if the optional params are present for the association, otherwise only A is created without the AB association.

If I leave all the logic in the controller then the controller gets pretty fat and a pain to test and the logic cannot be re-used easily. If I try to move the logic to the model A then I just have a random method for create_with_AB or something similar. It doesn't feel right.

It feels like there should be some layer in between? I'm not sure if there is a common pattern used to handle a situation like this?

Aucun commentaire:

Enregistrer un commentaire