简体   繁体   English

如何在轨道上使用红宝石验证外键?

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

My RoR 4 application manages an Organisations table in which several fields contain IDs pointing to parameters or users table. 我的RoR 4应用程序管理一个Organizations表,其中几个字段包含指向参数或用户表的ID。 Here is the description organisation.rb : 这是描述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

I want to make sure the model will always test the presence of foreign keys, so I wrote the following test in organisation_spec.rb : 我想确保模型将始终测试外键的存在,因此我在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

Test should be successful as parameters and user exist before the organisation is created. 创建组织之前,由于参数和用户存在,测试应该成功。 Unfortunately, running Rspec returns the same error for each foreign key: 不幸的是,运行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

How to correctly specify these foreign keys tests? 如何正确指定这些外键测试?

Thanks for your help 谢谢你的帮助

For foreign key (FK) constraints, I would recommend you do that in your database. 对于外键(FK)约束,建议您在数据库中进行。 Rails itself has no built-in support for that. Rails本身对此没有内置支持。 If you do want to check foreign keys' existence in Ruby/Rails, that would add unnecessary load to the application. 如果您确实想检查外键在Ruby / Rails中是否存在,那将给应用程序增加不必要的负担。

Here are several links might help: 以下几个链接可能会有所帮助:

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

Support for foreign key constraint in Rails 支持Rails中的外键约束

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

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

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