Ruby on rails 我们应该为模型中的关联设置验证器吗?
我对rails还很陌生,有些概念仍然让人困惑,所以我写了一个模型,定义了外键,然后还定义了验证器一些简单的验证器,比如一些字段不应该是空的。 例如:Ruby on rails 我们应该为模型中的关联设置验证器吗?,ruby-on-rails,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3.2,我对rails还很陌生,有些概念仍然让人困惑,所以我写了一个模型,定义了外键,然后还定义了验证器一些简单的验证器,比如一些字段不应该是空的。 例如: class KeyPerformanceInd < ActiveRecord::Base #attr_accessible :name, :organization_id, :target include ActiveModel::ForbiddenAttributesProtection belongs_to :organi
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
class-KeyPerformanceInd“kpi\u id”
验证:名称,状态:true
验证:目标,状态:true
验证:组织id,状态:true
结束
然后我想到的问题是,嗯,我是否也应该在这个模型中编写一些验证器,以确保我们用作外键的另一个表中的键也存在并且是有效的或类似的?还是我们在RSpec测试中做了什么?而不是在模型中?在这种情况下,我通常为关联编写rspec模型测试
Describe KeyPerformanceInd do
it {should belong_to(:key_performance_interval)}
end
谢谢,所以不需要在模型中也包含一些内容。是的,您必须在模型中拥有属于:关键性能间隔