Having a problem associating my results. Let me walk you though it:
I have 2 tables. dashboards, charts.
The charts table has a dashboard_id field as a dashboard has many charts.
So what I want in the dashboard controller is to grab the dashboard and all associated charts. Here is what I have so far:
Models
class Dashboards < ActiveRecord::Base
has_many :charts
....
end
class Charts < ActiveRecord::Base
has_one :dashboard
....
end
Contoller
class DashboardsController < ApplicationController
def show
#an ID is passed but for testing...
@dashboard = Dashboards.includes(:charts)
end
end
View
/dashboards/show.html.erb
<%=@dashboard.inspect%>
Result
uninitialized constant Dashboards::Chart
Can someone tell me what I’m doing wrong? It looks pretty clean to me and I have spent a few hours researching this. Am I overlooking something?
Firstly, model class names should use the singular (e.g.
Dashboard,Chart, etc). Also change theChartdashboard association to:Now in your controller (which generally uses the plural as you have it), this should work: