0

我对 Rails 很陌生,有些概念仍然令人困惑,所以我正在编写一个模型并定义了我的外键,然后我还定义了我的验证器一些简单的验证器,比如某些字段不应该为空。例如:

class KeyPerformanceInd < ActiveRecord::Base
  #attr_accessible :name, :organization_id, :target

  include ActiveModel::ForbiddenAttributesProtection

  belongs_to :organization
  has_many  :key_performance_intervals, :foreign_key => 'kpi_id'

  validates :name, presence: true
  validates :target, presence: true
  validates :organization_id, presence: true

end

然后想到的问题是,嗯,我是否也应该在这个模型中编写一些验证器,以确保我们用作 foreign_key 的另一个表中的键也存在并且是有效的或类似的东西?
或者这是我们在 RSpec 测试中所做的事情?而不是在模型中?

4

1 回答 1

1

我通常为关联编写 rspec 模型测试,在这种情况下

Describe KeyPerformanceInd do
  it {should belong_to(:key_performance_interval)}
end 
于 2013-02-06T04:35:27.410 回答