多态模型的唯一性验证失败

时间:2016-09-13 11:02:26

标签: validation ruby-on-rails-4 model

所以我有这个非常奇怪的问题。这是我的模特:

class Clip < ApplicationRecord
  belongs_to :owner, polymorphic: true
  default_scope -> { order(created_at: :desc) }
  validates :address, presence: true, uniqueness: { scope: [:owner_type, :owner_id] }
  before_save { self.adress=adress.split("=").last }
  validates :owner, presence: true

end

和测试

test "invalid creations" do
  get new_clip_path
  assert_no_difference "Clip.count" do
    post clips_path, params: { clip: { address: "",
                                     description: "bum bum bum" } }
  end
  assert_template "clips/new"
  assert_match "blank", response.body
  assert_no_difference "Clip.count" do
    #clip allready in db, should refure
    post clips_path, params: { clip: { address: "fWNaR-rxAic",
                                    description: "bum bum bum" } }
  end
  assert_template "clips/new"
  assert_no_match "blank", response.body
  assert_match "Address has already been taken", response.body
end

测试通过没有问题,但我只是偶然意识到,我仍然可以创建具有相同地址和所有者的剪辑,就像我想要的那样。这让我感到困惑有两个原因:a)此行的错误验证:地址,存在:真,唯一性:{范围:[:owner_type,:owner_id]}和b)为什么测试不会失败?

0 个答案:

没有答案