Ad

Can I Do STI And Still Use Polymorphic Path Helpers?

- 1 answer

I am using Single Table Inheritance and have comments on all the subclasses. I am only using 1 controller for all the different STI types. When the form_for helper generates a URL for a subtype it tries to use a helper for the subtype, but I want it to use the helper for the parent.

This is the error I get:

undefined method `subclasstypename_comments_path' for #<ActionView::Base:0x41ef27c>

The path helper it 'should' use is

parentclasstypename_comments_path
Ad

Answer

Yep, just use AR::Base#becomes.

Say your base class is Account, which is subclassed to GuestAccount and LoginAccount.

@account.is_a? LoginAccount? #=> true

Then you can just do a

form_for [@account.becomes(Account), @comment] do |f|
  ...
Ad
source: stackoverflow.com
Ad