0

我做了以下模型:

class Request < ActiveRecord::Base
end

class UrgentRequest < Request
  has_one:note
end

class Note < ActiveRecord::Base
  attr_accessible :request_id,.... 
  belongs_to :urgent_request, :foreign_key=>'request_id', :class_name=>'Request'
end

在我的控制器中,我设置了一个操作来创建一个UrgentRequest对象:

def new_scheduled_request
  @request = UrgentRequest.new
  @request.build_note #<-- getting error here

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @request }
  end
end

我收到以下错误:

ActiveRecord::UnknownAttributeError in RequestsController#new_urgent_request
unknown attribute: urgent_request_id

行号是我调用build_note呼叫的位置。页面上的表单应该是嵌套表单。这是怎么回事,我该如何解决?

4

1 回答 1

2

呃,没关系,我发现了这个问题。显然,我必须在 has_one:note 关联中的 UrgentRequests 模型中明确提及外键和类名参数。现在工作正常!

于 2012-05-27T07:41:24.420 回答