Ruby on rails 在控制器中建立关联的更好方法

Ruby on rails 在控制器中建立关联的更好方法,ruby-on-rails,collections,controller,Ruby On Rails,Collections,Controller,我需要父类的show方法中的链接来创建关联模型,因此我有以下代码: link_to "incomplete", new_polymorphic_path(part_c.underscore, :survey_id => survey.id) 在助手中 此链接到一个零件,该零件有如下新代码: # GET /source_control_parts/new def new get_collections if params[:survey_id] @s = Survey.fi

我需要父类的show方法中的链接来创建关联模型,因此我有以下代码:

link_to "incomplete", new_polymorphic_path(part_c.underscore, :survey_id => survey.id)
在助手中

此链接到一个零件,该零件有如下新代码:

# GET /source_control_parts/new
def new
  get_collections
  if params[:survey_id]
    @s = Survey.find(params[:survey_id])
    if @s.blank?
      @source_control_part = SourceControlPart.new
    else
      @source_control_part = @s.create_source_control_part
    end
  else
    @source_control_part = SourceControlPart.new
  end
end
我知道这不是很干。我如何简化这一点?有铁路吗

这个怎么样:

def new
  get_collections
  get_source_control_part
end

private
def get_source_control_part
  survey = params[:survey_id].blank? ? nil : Survey.find(params[:survey_id])
  @source_control_part = survey ? survey.create_source_control_part : SourceControlPart.new
end