私はこれでショービューを持っています:
<%= @application.application_name %>
<%= @application.application_field %>
そしてそれはこれを生成します:
Application name: New Employment App [#<ApplicationField id: 1, application_id: 1, applicant_id: nil, field_name: "Previous Job", field_type: "String", created_at: "2012-12-03 04:26:06", updated_at: "2012-12-03 04:26:06">, #<ApplicationField id: 2, application_id: 1, applicant_id: nil, field_name: "Previous Address", field_type: "String", created_at: "2012-12-03 04:26:06", updated_at: "2012-12-03 04:26:06">]
しかし、もしそうなら:
<%= @application.application_name %>
<%= @application.application_field.field_name %>
エラーが発生します:
undefined method `field_name' for #<ActiveRecord::Relation:0x007ff4ec822268>
なぜこのエラーが発生するのですか?
モデルは次のとおりです
class Application < ActiveRecord::Base
belongs_to :company
#has_many :applicants, :through => :application_field
has_many :application_field
accepts_nested_attributes_for :application_field, :allow_destroy => true
attr_accessible :application_name, :application_field_attributes
end
class ApplicationField < ActiveRecord::Base
belongs_to :application
has_many :application_fields_value
#belongs_to :applicant
attr_accessible :field_name, :field_type, :field_value, :application_field_values_attributes
accepts_nested_attributes_for :application_fields_value, :allow_destroy => true
end
コントローラーの表示アクション:
# GET /applications/1
# GET /applications/1.json
def show
@application = Application.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @application }
end
end