Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I do STI and still use polymorphic path helpers?

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
like image 541
Kyle Boon Avatar asked Mar 03 '09 02:03

Kyle Boon


1 Answers

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|
  ...
like image 180
ben_h Avatar answered Sep 20 '22 15:09

ben_h