7

How do turn off validation in Rails 3.2.3 in seeds.rb? I did this

u1 = User.create email: 'my@email.com', password: '123', validate: false

but it said Can't mass-assign protected attributes: validate. I know what it means. So how do I get rid of that error?

Alan Coromano
  • 24,958
  • 53
  • 135
  • 205
  • duplicate of http://stackoverflow.com/questions/2846314/ruby-on-rails-skipping-validate-on-create-statement-for-seeds – scones Apr 06 '13 at 09:14
  • it's not a duplicate at all! – Alan Coromano Apr 06 '13 at 10:13
  • You use the `create` function within seeds and want to turn of the validations for that. In the linked post, the User wishes to skip the validation on creation of objects in his seed files. That is an identical goal. Furthermore: the first answer in the linked question matches your question beautifully. – scones Apr 06 '13 at 10:16
  • why do I have to do all these complicated actions? – Alan Coromano Apr 06 '13 at 10:19
  • Because you try to act against the intentions of rails. The more you do so, the harder it gets to write code. The approach of @iltempo works as well. – scones Apr 06 '13 at 10:34
  • I know that it works well that's why I'll use it. – Alan Coromano Apr 06 '13 at 10:36

1 Answers1

19

You can do

u1 = User.new(email: 'my@email.com', password: '123').save(validate: false)
iltempo
  • 15,718
  • 8
  • 61
  • 72