`
zhangym124
  • 浏览: 339261 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

rails中单独更新某一属性,不走model层的validate

阅读更多

在做Rspec测试时,有一用例如下

 

  describe "validate update the pool name" do

     it "rejects duplicated names" do

 

       pool1 = machine_pool

       MachinePool.create!(attrs)

       pool1.update_attributes(attrs).should == false

       #如果用pool1.update_attribute(:name,"交易中心机器池").should == false则报错,猜测:只更新某一属性时,不走model层的验证

     end

  end

 

此用例的目的是测试某一张表的某一字段在更新时的唯一性,通过这一测试用例,发现一个从来没有意识到的问题。

当使用XXX.update_attributes时,会走model的验证功能,然而使用XXX.update_attribute时,却不走验证功能。

不知到我这么理解,有没有问题。

0
0
分享到:
评论
2 楼 zhangym124 2012-04-19  
attribute(name, value)
t284299773 写道
update_attribute(name, value)

Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular update_attribute method in Base is replaced with this when the validations module is mixed in, which it is by default.

update_attributes(attributes)

Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will fail and false will be returned.

确实如你所说


谢谢你细心的答案。
1 楼 t284299773 2012-04-19  
update_attribute(name, value)

Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular update_attribute method in Base is replaced with this when the validations module is mixed in, which it is by default.

update_attributes(attributes)

Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will fail and false will be returned.

确实如你所说

相关推荐

Global site tag (gtag.js) - Google Analytics