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
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.
1 Comment
bilash.saha
Thank you very much Emily. I did not know this.It will help a lot.