Polymorphic and Inverse_Of

99 views
Skip to first unread message

Anuj

unread,
May 30, 2015, 2:52:55 AM5/30/15
to rubyonra...@googlegroups.com
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!

Reply all
Reply to author
Forward
0 new messages