Question

In my Rails 3.2.6 app, I have a model representing a collection of data about a widget. To my mind, the correct name for this class is WidgetData, plural, because I have more than one item of data per widget.

If I ask Rails to generate a form for this class:

= form_for @widget_data do |f|
  ...

I get an error ActionView::Template::Error (undefined method 'widget_datum_path' .... Presumably this is because of the Rails data/datum inflection rule.

I'm unsure how best to resolve this: I could let Rails dictate that my model should in fact be WidgetDatum. Or I could somehow disable the use of the inflection rule in this particular case, but I'm unsure how best to do that. Advice welcome.

To anticipate one possible answer: the reason that the model is not just called Widget is that I have a façade class named that already, which presents a richer view of a widget incorporating both the WidgetData and other information as well.

Was it helpful?

Solution

Add this code to the file: config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|

   inflect.irregular 'widgetdata', 'widgetdatas' # or whatever you want your plural to be

 end

To address your comment on "plural, because I have more than one item of data per widget."

The idea is that each row of widget data is it's own object. Active Record will create one instance of WidgetData for each row of data. Your database table should be named (according to the Rails convention) WidgetData, because the table holds many WidgetDatums. But when in Rails, each row of data is instantiated as an individual object based on your model class. It will work either way as long as you configure your inflections correct. I typically only try to do this when building an interface upon an existing data model. But even then you will realize it is sometimes better to build a new rails data model on top of the existing model, using self.table_name = 'old table name convention' in your models.

OTHER TIPS

If you're looking to change the inflection rule in this case, you might have a look at this question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top