无法访问具有多对多关系的表中的属性

时间:2013-11-04 05:39:51

标签: ruby-on-rails ruby-on-rails-3

我在多对多关系中设置了两个表:maintenance_schedule和service_type。我正在尝试在maintenance_schedule的表单上创建一个下拉列表,允许我从service_type表中选择service_type_name。

我的模型设置如下:

class ServiceType < ActiveRecord::Base
    attr_accessible :service_type_name
    has_many :maintenance_schedules, :through =>  :schedule_services
end

class MaintenanceSchedule < ActiveRecord::Base
    attr_accessible :maintenance_schedule_date
    has_many :service_types, :through => :schedule_services
    accepts_nested_attributes_for :service_types
end

class ScheduleService < ActiveRecord::Base
    attr_accessible :maintenance_schedule_id, :service_type_id
    belongs_to :maintenance_schedule
    belongs_to :service_type
end

我的索引页面给了我未定义的方法`service_type_name'

<p id=<%="maintenance_schedule_#{maintenance_schedule.id}"%>>
<div>service type: <%=  maintenance_schedule.service_types.service_type_name %></div>
<%= link_to "Delete", maintenance_schedule_path(maintenance_schedule.id), :method => :delete, :class => "delete", :confirm => "Are you so sure?"%>
<%= link_to "Edit", edit_maintenance_schedule_path(maintenance_schedule)%>

这是我在控制器中的索引方法

def index
@maintenance_schedules = MaintenanceSchedule.all

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @maintenance_schedules}
end
end

0 个答案:

没有答案