错误:ActiveRecord :: UnknownAttributeError(Study的未知属性'study_type_question_group_id'):

时间:2016-08-11 16:21:40

标签: ruby-on-rails

运行我的应用程序时出现此错误:

ActiveRecord::UnknownAttributeError (unknown attribute 'study_type_question_group_id' for Study.):
app/controllers/protocols_controller.rb:46:in `create'

我已经完成了一些在线解决方案,但无法解决问题。列'id'在我的'study_type_question_group'表中可用。

My controller code : ( a fraction of my full code)
--------------------------------------------------
 def create

unless from_portal?
  @service_request = ServiceRequest.find session[:service_request_id]
end

@current_step = cookies['current_step']

new_protocol_attrs = params[:study] || params[:project] || Hash.new
@protocol = self.model_class.new(new_protocol_attrs.merge
                                    (study_type_question_group_id: StudyTypeQuestionGroup.active.pluck(:id).first))

@protocol.validate_nct = true

if @current_step == 'cancel'
  @current_step = 'return_to_service_request'
elsif @current_step == 'go_back'
  @current_step = 'protocol'
  @protocol.populate_for_edit
elsif @current_step == 'protocol' and @protocol.group_valid? :protocol
  @current_step = 'user_details'
  @protocol.populate_for_edit
elsif @current_step == 'user_details' and @protocol.valid?
  @protocol.save
  @current_step = 'return_to_service_request'
  flash[:notice] = "New #{@protocol.type.downcase} created"

  if @service_request
    @service_request.update_attribute(:protocol_id, @protocol.id) unless @service_request.protocol.present?
    @service_request.update_attribute(:status, 'draft')
    @service_request.sub_service_requests.each do |ssr|
      ssr.update_attribute(:status, 'draft')
    end
  end

  @current_step = 'return_to_service_request'
  flash[:notice] = "New #{@protocol.type.downcase} created"
else
  @protocol.populate_for_edit
end

cookies['current_step'] = @current_step

if @current_step != 'return_to_service_request'
  resolve_layout
end

我的项目中有很多文件,如果你想查看它们,你可以在这里查看:https://github.com/ctsiteam/cnmc-sparc-request

1 个答案:

答案 0 :(得分:0)

所以我在我的模型中添加了attr_accessor:study_type_question_group_id。现在它正在发挥作用。

似乎访问存在一些问题,因为我之前使用过attr_accessible,但现在我已经同时使用了。