如何对多种方法进行模型验证?
How make a Model validation on multiple methods?
我想在 :create
和 :update
上检查我的实体属性是否介于 0 和 5 之间。所以我在我的模型中添加了这样的验证:
class MyObject < ActiveRecord::Base
attr_accessible :first_attribute, :second_attribute
validate :check_attributes, on: :create and :update
private
def check_attributes
if self.first_attribute < 0 || self.first_attribute> 5
errors.add(:first_attribute, "first_attribute must be between 0 and 5")
end
if self.second_attribute < 0 || self.second_attribute > 5
errors.add(:second_attribute , "second_attribute must be between 0 and 5")
end
end
end
它适用于创建:当我尝试创建像 MyObject.create!(first_attribute: 7, second_attribute: 4)
这样的实体时,我收到错误消息。如果我输入 0 到 5 之间的值,它会创建实体。
但是当我更新像 my_entity.update_attributes!(first_attribute: 7)
这样的现有实体时,它允许更新,因为它没有进入验证函数。
我怎样才能让它适用于两种方法(创建和更新)?
你试过了吗
validate :check_attributes, :on => [:create, :update]
有了这个,验证将只在创建和更新操作中 运行。
这是 link 到 apidocs。
默认情况下,Rails 验证 运行 create
和 update
。所以它应该只是:
validate :check_attributes
请参阅 Rails Documentation。
仅当您想要验证 create
或 update
时才应使用 :on
选项。但是,如果您想对两者都进行验证,则无需指定 :on
选项。默认情况下,Rails 将对两者进行验证。
但是,有更好的方法来验证 0 到 5 之间的属性。您可以使用 Rails inclusion helper 来执行此操作,而不是定义您自己的自定义验证器。
validates :first_attribute,
inclusion: { in: 0..5, message: "first_attribute must be between 0 and 5" },
validates :second_attribute,
inclusion: { in: 0..5, message: "second_attribute must be between 0 and 5" }
该行应为
validate :check_attributes, on: [:create, :update]
但您也可以使用内置验证。另外 :create
和 :update
是 on
的默认值,所以你也可以省略它:
validates :first_attribute,
inclusion: { in: 0..5, message: "must be between 0 and 5" },
validates :second_attribute,
inclusion: { in: 0..5, message: "must be between 0 and 5" },
我想在 :create
和 :update
上检查我的实体属性是否介于 0 和 5 之间。所以我在我的模型中添加了这样的验证:
class MyObject < ActiveRecord::Base
attr_accessible :first_attribute, :second_attribute
validate :check_attributes, on: :create and :update
private
def check_attributes
if self.first_attribute < 0 || self.first_attribute> 5
errors.add(:first_attribute, "first_attribute must be between 0 and 5")
end
if self.second_attribute < 0 || self.second_attribute > 5
errors.add(:second_attribute , "second_attribute must be between 0 and 5")
end
end
end
它适用于创建:当我尝试创建像 MyObject.create!(first_attribute: 7, second_attribute: 4)
这样的实体时,我收到错误消息。如果我输入 0 到 5 之间的值,它会创建实体。
但是当我更新像 my_entity.update_attributes!(first_attribute: 7)
这样的现有实体时,它允许更新,因为它没有进入验证函数。
我怎样才能让它适用于两种方法(创建和更新)?
你试过了吗
validate :check_attributes, :on => [:create, :update]
有了这个,验证将只在创建和更新操作中 运行。 这是 link 到 apidocs。
默认情况下,Rails 验证 运行 create
和 update
。所以它应该只是:
validate :check_attributes
请参阅 Rails Documentation。
仅当您想要验证 create
或 update
时才应使用 :on
选项。但是,如果您想对两者都进行验证,则无需指定 :on
选项。默认情况下,Rails 将对两者进行验证。
但是,有更好的方法来验证 0 到 5 之间的属性。您可以使用 Rails inclusion helper 来执行此操作,而不是定义您自己的自定义验证器。
validates :first_attribute,
inclusion: { in: 0..5, message: "first_attribute must be between 0 and 5" },
validates :second_attribute,
inclusion: { in: 0..5, message: "second_attribute must be between 0 and 5" }
该行应为
validate :check_attributes, on: [:create, :update]
但您也可以使用内置验证。另外 :create
和 :update
是 on
的默认值,所以你也可以省略它:
validates :first_attribute,
inclusion: { in: 0..5, message: "must be between 0 and 5" },
validates :second_attribute,
inclusion: { in: 0..5, message: "must be between 0 and 5" },