简体   繁体   English

Rails模型:has_many,验证规则的顺序重要吗?

[英]Rails Models: Does the order of has_many, validation rules matter?

Curious about Rails models - in Hartl's Rails Tutorial, he has a User model that includes the following has_many :microposts attr_accessor :remember_token, :activation_token, :reset_token before_save :downcase_email before_create :create_activation_digest has_secure_password 对Rails模型感到好奇-在Hartl的Rails教程中,他有一个User模型,其中包括以下has_many :microposts attr_accessor :remember_token, :activation_token, :reset_token before_save :downcase_email before_create :create_activation_digest has_secure_password

My question is - does the order you put these in matter? 我的问题是-您将这些订单放入物质的顺序吗?

No not all. 不,不是全部。 You can declare these in anyone set of your choice. 您可以在自己选择的任何集合中声明它们。

I think that it is not necessary that validations follow any specific order. 我认为验证不必遵循任何特定顺序。 But you should keep your every validation flexible so that even if presence/length validation fails on any parameter, manipulation over it doesn't throw an error. 但是,您应该使每个验证都保持灵活,以便即使存在/长度验证对任何参数都失败,对其进行的操作也不会引发错误。
You can refer to this answer 你可以参考这个答案
Controlling the order of rails validations 控制Rails验证的顺序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM