rails中的has_one,belongs_to和create_before

时间:2014-03-26 14:44:09

标签: ruby-on-rails ruby-on-rails-4 scaffold

我用脚手架创建2个模型:

  1. 问题
  2. 记者
  3. 我想为每个问题创建1个记者,并使用before_create创建记者。

    我在数据库的问题表中按整数类型生成reporter_id。 以下代码显示问题和记者的行动和模型:

    problem.rb

    class Problem < ActiveRecord::Base
      has_one :reporter
      before_create :build_reporter
    end
    

    reporter.rb

    class Reporter < ActiveRecord::Base
    
      belongs_to :problem
    
    end
    

    problems_controller.rb

    class ProblemsController < ApplicationController
    
      before_action :set_problem, only: [:show, :edit, :update, :destroy]
    
      def index
        @problems = Problem.all
      end
    
      def show
      end
    
      def new
        @problem = Problem.new
      end
    
      def edit
      end
    
      def create
        @problem = Problem.new(problem_params)
    
        respond_to do |format|
          if @problem.save
            format.html { redirect_to @problem, notice: 'Problem was successfully created.' }
            format.json { render action: 'show', status: :created, location: @problem }
          else
            format.html { render action: 'new' }
            format.json { render json: @problem.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def update
        respond_to do |format|
          if @problem.update(problem_params)
            format.html { redirect_to @problem, notice: 'Problem was successfully updated.' }
            format.json { head :no_content }
          else
            format.html { render action: 'edit' }
            format.json { render json: @problem.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def destroy
        @problem.destroy
        respond_to do |format|
          format.html { redirect_to problems_url }
          format.json { head :no_content }
        end
      end
    

    私人     def set_problem       @problem = Problem.find(params [:id])     端

    def problem_params
      params.require(:problem).permit(:reporter_id, :describe_id, :datetime, :trace_code, :status)
    end
    

    reporter_controller.rb

    class ReportersController < ApplicationController
      before_action :set_reporter, only: [:show, :edit, :update, :destroy]
    
      def index
        @reporters = Reporter.all
      end
    
      def show
      end
    
      def new
        @reporter = Reporter.new
      end
    
      def edit
      end
    
      def update
        respond_to do |format|
          if @reporter.update(reporter_params)
            format.html { redirect_to @reporter, notice: 'Reporter was successfully updated.' }
            format.json { head :no_content }
          else
            format.html { render action: 'edit' }
            format.json { render json: @reporter.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def destroy
        @reporter.destroy
        respond_to do |format|
          format.html { redirect_to reporters_url }
          format.json { head :no_content }
        end
      end
    
      private
        def set_reporter
          @reporter = Reporter.find(params[:id])
        end
    
        def reporter_params
          params.require(:reporter).permit(:user_name, :gomrok_name, :phone_number)
        end
    end
    

    当我在localhost中选择新问题按钮时:3000 /问题,显示文本字段,但是当完成文本字段并选择创建问题按钮时,我收到此错误:

    ActiveRecord::UnknownAttributeError in ProblemsController#create 
    
    
    unknown attribute: problem_id
    Extracted source (around line #31):   
    
    
    respond_to do |format|
      if @problem.save
        format.html { redirect_to @problem, notice: 'Problem was successfully created.' }
        format.json { render action: 'show', status: :created, location: @problem }
      else
    

    问题是什么?!

    我没有使用它。

    我想在数据库的问题表中设置记者ID;如何使用before_create或after_create创建报告器并将问题表中的reporter_id保存?

1 个答案:

答案 0 :(得分:0)

您错误地设置了关系。你拥有的是reporter_id表中的problems列。在1-1关系中创建了foreign_key关系belongs_to。因此,您的代码应如下所示:

class Problem < ActiveRecord::Base
  belongs_to :reporter
end

class Reporter < ActiveRecord::Base    
  has_one :problem   
  before_create :build_problem
end