mercredi 28 juin 2017

has_many polymorphic relationship where our _type field is an int?

We have an app in Rails 4 (but soon to be Rails 5) that has a polymorphic log class that we inherited from a Django app.

In a traditional polymorphic relation, the objects would have source_type and source_id, which would be things like 'some_class' and 123.

In our little model, we have content_type and content_id which unfortunately map to two ints, where the int in content_type further points to a ContentType model that -- to further complicate things -- dont map to Rails-ey types ('some_type' but rather django-ey 'some type class'.

It's a mess.

Is there any way to override the value used in looking in a polymorphic association (e.g. instead of using 'some_class', use 11) -- but just for the association? There is an answer for a problem somewhat similar to ours, but it is 4 years old and it overrides association_class. We still use some of this strange Django voodoo elsewhere in our codebase, so overrides feel like they'd cause more problems than solve.

Aucun commentaire:

Enregistrer un commentaire