导致外键= nil的原因是什么?

时间:2013-07-15 17:01:59

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

生成器has_many结果 结果belongs_to generator

当我运行rails console:Result.find(1)时,generator_id = nil。为什么这样???当我创建数据时,路由是这样的: 本地主机:3000 /发生器/ 1 /结果/新

以下代码没有错误。我怎么在我的Generator / index.html

中有这个
<% generator.results.each do |result| %>
        <td><%= result.ncbi_ref_seq %></td>
        <td><%= result.genome_sample %></td>
        <td><%= result.binding_times %></td>
<%end%> 
他们是空的。而@ generators.each做| generator |给了我发电机的价值。换句话说,属于此生成器的结果值为空,但显示的是生成器值。我想要的是显示所有生成器及其结果值。 some1可以帮助我吗?

Generator.model

class Generator < ActiveRecord::Base
  has_many :results , :dependent => :destroy
  attr_accessible :choice, :primer_length, :random_primer_generated, :genome_sample, :generator_id

GeneratorController.rb

def index
        @generators = Generator.all
      end

def create    
    @generator = Generator.new(generator_params)
    @generator.choice = params[:choice]
      if params[:choice] == 'Randomly'
          @generator.random_generate(generator_params)
      elsif params[:choice] == 'No_of_ATGC'
          @generator.no_ATGC(params[:no_A],params[:no_T],params[:no_G],params[:no_C])
      elsif params[:choice] == 'Seating'
          @generator.seating(params[:user_seq])
      end

    @generator.result_choice=params[:result_choice]
    respond_to do |format|
      if @generator.save
          if @generator.result_choice == 'Yes'
            format.html { redirect_to(new_generator_result_path(@generator)) }
          else
            format.html { redirect_to @generator, notice: 'Result was successfully created.' }
            format.json { render action: 'show', status: :created, location: @generator }
          end
      else 
        format.html { render action: 'new' }
        format.json { render json: @generator.errors, status: :unprocessable_entity }
      end
    end
  end

结果模型

class Result < ActiveRecord::Base
   attr_accessible :ncbi_ref_seq,:genome_seq, :genome_sample
   belongs_to :generator, foreign_key: "generator_id"

ResultController

def index
    @results = Result.all
  end
def create
    @result = Result.new(result_params)
    @result.generate_result(result_params)
    respond_to do |format|
      if @result.save
        format.html { redirect_to @result, notice: 'Result was successfully created.' }
        format.json { render action: 'show', status: :created, location: @result }
      else
        format.html { render action: 'new' }
        format.json { render json: @result.errors, status: :unprocessable_entity }
      end
    end
  end

schema.rb

create_table "generators", force: true do |t|
    t.integer  "primer_length"
    t.integer  "no_A"
    t.integer  "no_T"
    t.integer  "no_G"
    t.integer  "no_C"
    t.string   "choice"
    t.string   "random_primer_generated"
    t.string   "user_seq"
    t.string   "c_primer"
    t.string   "result_choice"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "results", force: true do |t|
    t.integer  "generator_id"
    t.string   "ncbi_ref_seq"
    t.string   "genome_seq"
    t.string   "genome_sample"
    t.integer  "binding_times"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "results", ["generator_id"], name: "index_results_on_generator_id"

  create_table "results_{:column_options=>{:null=>true}}", id: false, force: true do |t|
    t.integer  "result_id",                           null: false
    t.integer  "{:column_options=>{:null=>true}}_id", null: false
    t.integer  "generator_id"
    t.string   "ncbi_ref_seq"
    t.string   "genome_seq"
    t.string   "genome_sample"
    t.integer  "binding_times"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "results_{:column_options=>{:null=>true}}", ["generator_id"], name: "index_results_{:column_options=>{:null=>true}}_on_generator_id"

0 个答案:

没有答案