Rails Model inheritance in forms

Posted by Tiago on Stack Overflow See other posts from Stack Overflow or by Tiago
Published on 2011-02-18T23:23:03Z Indexed on 2011/02/18 23:25 UTC
Read the original article Hit count: 241

I'm doing a reporting system for my app. I created a model ReportKind for example, but as I can report a lot of stuff, I wanted to make different groups of report kinds. Since they share a lot of behavior, I'm trying to use inheritance.

So I have the main model:

model ReportKind << ActiveRecord::Base
end

and created for example:

model UserReportKind << ReportKind
end

In my table report_kinds I've the type column, and until here its all working. My problem is in the forms/controllers.

When I do a ReportKind.new, my form is build with the '*report_kind*' prefix. If a get a UserReportKind, even through a ReportKind.find, the form will build the 'user_report_kind' prefix.

This mess everything in the controllers, since sometimes I'll have params[:report_kind], sometimes params[:user_report_kind], and so on for every other inheritance I made.

Is there anyway to force it to aways use the 'report_kind' prefix? Also I had to force the attribute 'type' in the controller, because it didn't get the value direct from the form, is there a pretty way to do this?

Routing was another problem, since it was trying to build routes based in the inherited models names. I overcome that by adding the other models in routes pointing to the same controller.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails-3