Ruby on rails 如何在Rails中验证布尔值
在我的应用程序中,我的Authors表中有一个布尔值。如何验证该布尔值可以设置为false的次数Ruby on rails 如何在Rails中验证布尔值,ruby-on-rails,Ruby On Rails,在我的应用程序中,我的Authors表中有一个布尔值。如何验证该布尔值可以设置为false的次数 我现在的代码不能正常工作 class Author < ActiveRecord::Base validate :book_limit, :on => :update def book_limit if self.user.authors(:reload).count(:conditions => { :book => true })
我现在的代码不能正常工作
class Author < ActiveRecord::Base
validate :book_limit, :on => :update
def book_limit
if self.user.authors(:reload).count(:conditions => { :book => true }) >= 15
errors.add(:base, "Exceeds book limit: Book 15/15")
end
end
end
类作者:update
def账面限额
如果self.user.authors(:reload).count(:conditions=>{:book=>true})>=15
错误。添加(:base,“超出书本限制:书本15/15”)
结束
结束
结束
我相信这就是你想要的:
class Author < ActiveRecord::Base
validate :book_limit, :on => :update
def book_limit
if self.user.authors.where(book: true).count >= 15
errors.add(:base, "Exceeds book limit: Book 15/15")
end
end
end
类作者:update
def账面限额
如果self.user.authors.where(book:true).count>=15
错误。添加(:base,“超出书本限制:书本15/15”)
结束
结束
结束
@MichaelSzyndel试图这么做,但我还是等了15分钟才这么做……啊,也许吧。所以有时候很烦人;)等等,每次我删除一个处于该限制的布尔值时,我仍然会收到验证消息,因此无法删除它@henrikhodnenvm,我只是决定在15之后隐藏布尔值。。再次感谢@henrikhodne