简体   繁体   English

通过外键链接rails对象

[英]linking rails objects through foreign keys

What is the best way to link rails objects through foreign keys? 通过外键链接Rails对象的最佳方法是什么? Here is my example. 这是我的例子。 a Practitioner has a timecard (collect hours worked per day). 从业者有一个时间表(收集每天的工作时间)。 I want to put the practitioner's id on the appointment to show this: 我想在预约中加上医生的身分证明:

from routes.rb 来自routes.rb

  resources :locations do
    resources :practitioners, only: [:new, :create]
  end

  resources :practitioners, except: [:new, :create] do
    resources :timecards, except: :delete
    resources :appointments
  end

timecard.rb - you can see here I forced the timecard.practitioner_id to be 3, but that was a brute force testing effort. timecard.rb-您可以在此处看到我将timecard.practitioner_id设置为3,但这是蛮力的测试工作。

class Timecard < ActiveRecord::Base
  belongs_to :practitioner
 attr_protected :id

  before_save :set_practitioner_id

  def set_practitioner_id
    if self.practitioner_id.blank?
      self.practitioner_id = 3
    end
  end
end

And from my controller... 从我的控制器...

def create
    @practitioner = Practitioner.find(params[:practitioner_id])
    @timecard = Timecard.create(params[:timecard])


    respond_to do |format|
      if @timecard.save
        format.html { redirect_to @practitioner, notice: 'Timecard was successfully created.' }
        format.json { render json: @timecard, status: :created, location: @timecard }
      else
        format.html { render action: "new" }
        format.json { render json: @timecard.errors, status: :unprocessable_entity }
      end
    end
  end

I can see the parameter for the practitioner_id = 3 (which is what I want to use), I just don't know how to access it in the code. 我可以看到该操作者的id = 3的参数(这是我要使用的参数),我只是不知道如何在代码中访问它。

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"DIp4R31McXnBNP4NB06KDTzV+8lLcWvxbq1w9J+Z1+M=",
 "timecard"=>{
   "hours_worked"=>"2",
   "activity_date(1i)"=>"2013",
   "activity_date(2i)"=>"3",
   "activity_date(3i)"=>"20",
   "practitioner_id"=>"",
   "location_id"=>"1"
 },
 "commit"=>"Create Timecard",
 "practitioner_id"=>"3"   <== this is what I want to use
}

If I understand your question, you can simply set @timecard.practitioner = @practitioner before saving your timecard in the controller. 如果我理解您的问题,则可以在将时间卡保存到控制器之前简单地设置@timecard.practitioner = @practitioner You can do that because belongs_to gives you a practioner= method that "Assigns the associate object, extracts the primary key, and sets it as the foreign key." 您可以这样做,因为belongs_to 为您提供了一个practioner=方法,该方法“分配关联对象,提取主键,并将其设置为外键”。

I'm assuming that a practitioner has many timecards so you should be able to do something like 我假设一个从业者有很多时间表,所以您应该可以做类似的事情

# practitioner.rb
has_many :timecards

# controller
@practitioner = Practitioner.find(params[:practitioner_id])
@timecard = @practitioner.timecards.build(params[:timecard])

having the association set up will take care of setting the foreign keys for you. 建立关联将为您设置外键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM