2013-02-06 51 views
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測試中做了什麼?而不是在模型中?

回答

1

我常寫RSpec的關聯模型試驗,在這種情況下

Describe KeyPerformanceInd do 
    it {should belong_to(:key_performance_interval)} 
end 
+0

感謝,所以不需要也有模型的一些東西。 – Bohn

+0

是的,你必須在模型中有belongs_to:key_performance_interval – sameera207

相關問題