关联结果时出现问题。让我带你走过:
我有两张表。仪表板、图表。
图表表有一个dashboard_id字段,因为仪表板有许多图表。
我想在仪表板控制器中获取仪表板和所有相关的图表。以下是目前为止的内容:
class Dashboards < ActiveRecord::Base
has_many :charts
....
end
class Charts < ActiveRecord::Base
has_one :dashboard
....
end
控制器
class DashboardsController < ApplicationController
def show
#an ID is passed but for testing...
@dashboard = Dashboards.includes(:charts)
end
end
<<p> 视图/strong> /仪表板/show.html.erb
<%=@dashboard.inspect%>
结果uninitialized constant Dashboards::Chart
谁能告诉我我做错了什么?它看起来很干净,我花了几个小时研究这个。我是不是忽略了什么? 首先,模型类名称应该使用单数(例如Dashboard
, Chart
等)。同时将Chart
仪表板关联更改为:
class Chart < ActiveRecord::Base
belongs_to :dashboard
....
end
现在在控制器中(通常使用复数形式),这应该可以工作:
class DashboardsController < ApplicationController
def show
#an ID is passed but for testing...
@dashboard = Dashboard.includes(:charts)
end
end