[英]rails 2.3.8 validates_uniqueness_of with :scope not working
[英]Rails validates_uniqueness_of and scope - working only on update
我有以下3個課程:
class Profile < ActiveRecord::Base
end
class Subject < ActiveRecord::Base
end
class ProfileSubject < ActiveRecord::Base
belongs_to :profile
belongs_to :subject
validates_uniqueness_of :subject_id, scope: :profile_id
end
當我用其關聯的ProfileSubject集合更新我的個人資料時, validates_uniqueness_of
效果很好。 但是,在創建操作時-尚未驗證此唯一性。 我想,也許這是因為當我創建對象時,我還沒有profile_id ...並且我嘗試為模型添加我自己的令牌,我可以用它們來連接它們並通過它進行validates_uniqueness_of
(在validates_uniqueness_of
作用域上使用它)。 我現在知道的廣告,它沒有用,也不起作用。
您能否幫我...為什么此標准驗證對更新操作有效,但對創建操作無效。
創建代碼是標准的,如下所示:
@profile = Profile.new(profile_params)
if @profile.save
# ...
else
# ...
end
采用:
validates_associated :subject, :profile
validates :subject, :profile, :presence => true
代替:
validates_uniqueness_of :subject_id, scope: :profile_id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.