Ruby on Rails:跳过种子的“validate_on_create"语句 [英] Ruby on Rails: Skipping 'validate_on_create' statement for seeds

查看:34
本文介绍了Ruby on Rails:跳过种子的“validate_on_create"语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个控制器中有一个validate_on_create"语句,我希望跳过所有种子数据.有哪些解决方案可以让我的种子文件中的 create 语句跳过此验证.我当前的解决方案是在每次运行 rake db:seed 时注释掉验证.还有更聪明的吗?

I have a 'validate_on_create' statement in one of my controllers that I would like all of my seed data to skip. What are some solutions so that the create statement in my seeds file skips this validation. My current solution is commenting out the validation each time I run rake db:seed. Anything a little more clever?

推荐答案

您是否考虑在模型中添加一个在 validate_on_create 方法中检查的属性?

Have you considered adding an attribute in the model which is checked in the validate_on_create method?

示例:

class MyModel < ActiveRecord::Base

  attr_accessor :skip_on_create_validation

  def validate_on_create
    unless skip_on_create_validation
      # do validation
    end
  end

end

# In db/seeds.rb
MyModel.create(:skip_on_create_validation => true, ......)

这篇关于Ruby on Rails:跳过种子的“validate_on_create"语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆