如何在rails上使用ruby验证外键?

时间:2016-01-04 22:55:40

标签: ruby-on-rails rspec

我的RoR 4应用程序管理一个组织表,其中多个字段包含指向参数或用户表的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

如何正确指定这些外键测试?

感谢您的帮助

1 个答案:

答案 0 :(得分:-1)

对于外键(FK)约束,我建议您在数据库中执行此操作。 Rails本身没有内置的支持。如果你确实想检查Ruby / Rails中是否存在外键,那么会给应用程序增加不必要的负担。

以下几个链接可能有所帮助:

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

Support for foreign key constraint in Rails

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