簡體   English   中英

如何在軌道上使用紅寶石驗證外鍵?

[英]How to validate foreign keys with ruby on rails?

我的RoR 4應用程序管理一個Organizations表,其中幾個字段包含指向參數或用戶表的ID。 這是描述organisation.rb

# Table name: organisations
#
#  id          :integer          not null, primary key
#  name        :string(100)      not null
#  description :text
#  address     :text
#  zip         :string(20)
#  city        :string(100)
#  state       :string(100)
#  country_id  :integer
#  website     :string(100)
#  email       :string(100)
#  phone       :string(100)
#  categories  :text
#  status_id   :integer          default(0), not null
#  legal_id    :integer          default(0), not null
#  owner_id    :integer          not null
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  created_by  :string(100)      not null
#  updated_by  :string(100)      not null
#  session_id  :string(100)      not null
#  code        :string(100)
#

class Organisation < ActiveRecord::Base

### validations
  validates :name,       presence: true, length: { minimum: 5 }
  validates :created_by, presence: true
  validates :updated_by, presence: true
  validates :session_id, presence: true
  belongs_to :owner, :class_name => "User", :foreign_key => "owner_id"               # helps retrieving the owner name
    validates :owner, presence: true
  belongs_to :status, :class_name => "Parameter", :foreign_key => "status_id"        # helps retrieving the parameter
    validates :status, presence: true
  belongs_to :legal, :class_name => "Parameter", :foreign_key => "legal_id"      # helps retrieving the parameter
    validates :legal, presence: true
end

我想確保模型將始終測試外鍵的存在,因此我在organisation_spec.rb中編寫了以下測試:

require 'rails_helper'

RSpec.describe Organisation, type: :model do

  describe 'Validations'
    context 'With existing parameters and user' do
    FactoryGirl.build(:parameter)
    FactoryGirl.build(:user)
    subject {FactoryGirl.build(:organisation)}
    it {should validate_presence_of(:name)}
    it {should validate_length_of(:name).is_at_least(5)}
    it {should belong_to(:status).class_name('parameter')}
    it {should belong_to(:legal).class_name('parameter')}  
    it {should belong_to(:owner).class_name('user')}
    it {should validate_presence_of(:created_by)}  
    it {should validate_presence_of(:updated_by)}
    it {should validate_presence_of(:session_id)}  
    end
end

創建組織之前,由於參數和用戶存在,測試應該成功。 不幸的是,運行Rspec為每個外鍵返回相同的錯誤:

rspec ./spec/models/organisation_spec.rb:39 # Organisation With existing parameters and user should belong to status class_name => parameter
rspec ./spec/models/organisation_spec.rb:40 # Organisation With existing parameters and user should belong to legal class_name => parameter
rspec ./spec/models/organisation_spec.rb:41 # Organisation With existing parameters and user should belong to owner class_name => user

如何正確指定這些外鍵測試?

謝謝你的幫助

對於外鍵(FK)約束,建議您在數據庫中進行。 Rails本身對此沒有內置支持。 如果您確實想檢查外鍵在Ruby / Rails中是否存在,那將給應用程序增加不必要的負擔。

以下幾個鏈接可能會有所幫助:

http://blog.weiskotten.com/2008/01/you-should-use-foreign-key-constraints-in-rails.html

支持Rails中的外鍵約束

http://complicated-simplicity.com/2009/06/fk-constraints/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM