我在驗證派生類中關聯的存在時面臨奇怪的問題。驗證模型的子類中的關聯
Class Contact < ActiveRecord::Base
has_and_belongs_to_many :things
end
Class SubContact < Contact
# validates_presence_of :things
validate :must_have_things
def must_have_things
if things.blank?
errors.add("Must select things")
end
end
end
在SubContact
類,我想驗證的事情存在,但沒有什麼工作。我已經嘗試過自定義和內置驗證程序 我該如何實現這一目標?
@alexanderbird有問題OP從未提及過STI,所以我不認爲這個鏈接是有用的 –
OP的示例代碼是否顯示STI?我猜如果聯繫人表中沒有類型列,那麼它不是STI ...... – alexanderbird
是的,我們沒有任何類型的列。 –