1

post.rb

  @allowed_post_types = [
    'type1',
    'type2',
    'type3',
    'type4'
  ]

  #validate post type
  validates :post_type, :presence => true, :inclusion=> { :in => @allowed_post_types }, :if => :valid_post_type?



  def valid_post_type?
    self.post_type != 'type5'
  end

validates :post_type每当:post_type不是type5时,我如何启用代码行?

尝试了上述方法,但似乎不起作用

4

1 回答 1

2

尝试:

validates :post_type, 
          :presence => true, 
          :inclusion=> { :in => @allowed_post_types }, 
          :if => lambda { |a| a[:post_type] != "type5" }
于 2012-09-13T17:23:52.800 回答