Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails data/datum inflection issue

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.

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.

like image 274
Ian Dickinson Avatar asked Jun 25 '12 15:06

Ian Dickinson


Video Answer


1 Answers

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.

like image 91
OpenCoderX Avatar answered Sep 19 '22 10:09

OpenCoderX