Ruby on Rails Sunday, January 13, 2019

I think I answered my own Q. Looking at the "type" field for STI I forgot that models are not one-to-one mapped to tables, so we would have to deal with that too. So perhaps that's the root issue. We'd have to provide explicit configuration for mapping classes to tables for every model that could be referenced in this manner. I guess that would be un-rails-like.

So perhaps the best we can do is make migration easier. Looks like there's no way around just dealing with the DB layer once we open the "type"-like can of worms.

On Saturday, January 12, 2019 at 10:25:12 PM UTC-8, Christophe C wrote:
I haven't found an exact statement of this problem yet, but if you find one please direct me accordingly.

My one problem with polymorphic associations is that your code is now in the database. What happens when you rename a class, or re-scope it's module?

Since class name changes are much more common than table name changes (citation needed), or at the very least you're not in the DB layer of the world and may perhaps not think of it (whereas the inverse may be true), I think it makes sense to add an option to reference the polymorphic relationship by table.

E.g.

polymorphable_id
polymorphable_table # or polymorphable_table_name

I haven't looked into how one would retrieve the class name from the table, but based on this SO post one would have to find a way to perhaps cache the model to table name. Models are all loaded anyway, so we could create a reverse-lookup.

Even with thousands of models, I don't think this is unreasonable for the convenience, but I'm open to being wrong.

Thoughts?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/57753328-ccd2-40f7-aa61-d752c9e1b8b3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment