I have a simple has_many/belongs_to relationship between Report and Chart. The issue I’m having is that my Chart model is a parent that has children.
So in my Report model I have
class Report < ActiveRecord::Base
has_many :charts
end
And my Chart model is a parent, where Pie, Line, Bar all inherit from Chart. I’m not sure where the belongs_to :report belongs within the chart model, or children of chart model. I get errors when I attempt to access chart.report because the object is of type “Class”
undefined local variable or method `report’ for #< Class:0x104974b90>
The Chart model uses STI so its pulling say.. ‘Gender’ from the chart_type column in the charts table.. what am I missing?
EDIT
Chart
/ \
Pie Line
/ \
/ \
Gender Sex
I am (using STI) instantiating an object of type Gender, or Sex. Hopefully this helps a bit more.
I have a feeling that its caused by
@chart.update_attributes(params[:chart])
because when submitted its actually params[:chart] its params[:gender] or params[:sex]
Your problem is that you have one controller receiving all these different types of models. When you use the Rails form helpers, they’re taking the object type from the instance and using that to populate the params. This means that you have
params[:gender]instead ofparams[:chart]You can fix this by overriding the method that Rails uses to generate the form. Put the following code in your base chart class:
Now, any class that is a child of Chart will be passed into your receiving action as
params[:chart]Keep in mind that changing the object name as I’ve outlined above could break functionality of some plugins/gems that rely on it. You should look into having multiple controllers. Instead of having a
ChartsControllerto receive all of the data, have aGenderChartsControllerand aLineChartsController. This provides two benefits: