Relational data / zeros issues

In my Rails 3.2.6 application, I have a model representing a widget dataset. In my opinion, the correct name for this class is WidgetData , the plural, because I have more than one data item for each widget.

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

 = form_for @widget_data do |f| ... 

I get the error ActionView::Template::Error (undefined method 'widget_datum_path' ... Presumably this is due to the data / data bending rule.

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

To anticipate one possible answer: the reason the model is not just called Widget is because I already have a facade class with a name that represents a richer view of the widget that includes both WidgetData and other information as well.

+6
source share
2 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 answer your comment "plural because I have multiple data elements for each widget."

The idea is that each row of these widgets is its own object. Active Record will create one instance of WidgetData for each row of data. Your database table must be named (according to the Rails convention) WidgetData because the table contains a lot of WidgetDatums. But when in Rails every row of data is created as a separate object based on your model class. It will work anyway, as long as you properly configure your excesses. Usually I try to do this only when building an interface with an existing data model. But even then you will realize that sometimes it’s better to create a new rail data model on top of an existing model using self.table_name = 'old table name convention' in your models.

+10
source

If you want to change the inflection rule in this case, you can look at this question .

+1
source

Source: https://habr.com/ru/post/918957/


All Articles