Ruby on rails 如果中的else在rails中验证

Ruby on rails 如果中的else在rails中验证,ruby-on-rails,Ruby On Rails,在模型中,我想要下面这样的东西 validates :category, :presence => true if self.post = "this_is_post" 是否可能,或者我必须在保存此检查之前使用钩子方法?这应该可以: validates :category, :presence => true, :if => ->(a){ a.post == 'this_is_post' } 这应该起作用: validates :category, :presence

在模型中,我想要下面这样的东西

validates :category, :presence => true if self.post = "this_is_post"
是否可能,或者我必须在保存此检查之前使用钩子方法?

这应该可以:

validates :category, :presence => true, :if => ->(a){ a.post == 'this_is_post' }
这应该起作用:

validates :category, :presence => true, :if => ->(a){ a.post == 'this_is_post' }
这应该起作用:

validates :category, :presence => true, :if => ->(a){ a.post == 'this_is_post' }
这应该起作用:

validates :category, :presence => true, :if => ->(a){ a.post == 'this_is_post' }

在这里,rails中验证条件的多个代码snippest:

 class Person < ActiveRecord::Base
    validates :surname, presence: true, if: "name.nil?"
 end
=================================

class Person < ActiveRecord::Base
   validates :category, presence: true, if: "self.post.eql?('this_is_post')"
 end
class-Person
这里,rails中验证条件的多个代码snippest:

 class Person < ActiveRecord::Base
    validates :surname, presence: true, if: "name.nil?"
 end
=================================

class Person < ActiveRecord::Base
   validates :category, presence: true, if: "self.post.eql?('this_is_post')"
 end
class-Person
这里,rails中验证条件的多个代码snippest:

 class Person < ActiveRecord::Base
    validates :surname, presence: true, if: "name.nil?"
 end
=================================

class Person < ActiveRecord::Base
   validates :category, presence: true, if: "self.post.eql?('this_is_post')"
 end
class-Person
这里,rails中验证条件的多个代码snippest:

 class Person < ActiveRecord::Base
    validates :surname, presence: true, if: "name.nil?"
 end
=================================

class Person < ActiveRecord::Base
   validates :category, presence: true, if: "self.post.eql?('this_is_post')"
 end
class-Person
您能解释一下这里的a是什么吗。在模型中,我使用self.post访问任何资源字段。@user3128796如果您在此
中传递
Proc
对象:如果
选项,则使用传入的模型对象对其进行计算(在我的示例中,它用
a
引用)。所以你可以简单地调用它
post
,就像你调用“常规”对象一样。你能解释一下这里的a是什么吗。在模型中,我使用self.post访问任何资源字段。@user3128796如果您在此
中传递
Proc
对象:如果
选项,则使用传入的模型对象对其进行计算(在我的示例中,它用
a
引用)。所以你可以简单地调用它
post
,就像你调用“常规”对象一样。你能解释一下这里的a是什么吗。在模型中,我使用self.post访问任何资源字段。@user3128796如果您在此
中传递
Proc
对象:如果
选项,则使用传入的模型对象对其进行计算(在我的示例中,它用
a
引用)。所以你可以简单地调用它
post
,就像你调用“常规”对象一样。你能解释一下这里的a是什么吗。在模型中,我使用self.post访问任何资源字段。@user3128796如果您在此
中传递
Proc
对象:如果
选项,则使用传入的模型对象对其进行计算(在我的示例中,它用
a
引用)。因此,您只需调用它
post
,就像“常规”对象一样。最后一个不工作,但我看起来像最后一个不工作,但我看起来像最后一个不工作,但我看起来像最后一个不工作,但我看起来像最后一个不工作,但我看起来像最后一个