Ruby on Rails Friday, May 29, 2015

Hello everyone,

Rails documentation mentions that 'inverse_of' associations don't work with polymorphic associations. I searched quite a bit online but couldn't find a good reasoning for why that is the case? 

My use-case for using inverse_of is to be able to have nested models. And the (child) model that I nested, was supposed to be the polymorphic model. For example, 

BOOK
has_many :pages,
-> { order(position: :asc) },
inverse_of
: :book

accepts_nested_attributes_for
:pages

PAGE
belongs_to :post, inverse_of: :content_sections
 validates
:post, presence: true

Now I am going to have other models like Documents, Journals, Photo-Albums all of whom need to have Pages. This seemed to be a perfect case to make a polymorphic relationship between Page and all these other models using Pageable.

In such a case, I would like to understand why rails polymorphism doesn't play well with inverse_of, and if there are any other solutions to solve my use-case? Should I go ahead and make my own version of rails polymorphism for this use-case?

Thank you!

--
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/1bbc4c75-41d1-485e-a1a5-322d5a5605de%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment