2

I have a migration for creating a table with around 15 fields in it and all of them should not be null. I was wondering if is there any trick to do that at once instead of declaring :null => false for every single field.

1 Answer 1

6

Actually, you can do this using with_options. It's most commonly used in routes and setting up validations, but it will actually work on any method that takes an options hash as the last argument. So, something like:

create_table :foo do |t| 
  t.with_options :null => false do |opt|
    opt.string :column_name
    opt.string :other_column_name
  end
end

Here's the documentation on Object#with_options.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Emily. I did not know this.It will help a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.