0

私はレールにかなり慣れていないため、いくつかの概念はまだ混乱しているため、モデルを作成して外部キーを定義し、いくつかのフィールドを空白にしないなどの簡単なものをバリデーターにも定義しました。例えば:

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 に答える