Skip to content

Instantly share code, notes, and snippets.

@johanb
Last active December 12, 2015 00:08
Show Gist options
  • Save johanb/4681127 to your computer and use it in GitHub Desktop.
Save johanb/4681127 to your computer and use it in GitHub Desktop.
def change
create_table :ingredients do |t|
t.integer :quantity, default: 0, null: false
t.timestamps
end
end
def change
create_table :ingredients do |t|
t.integer :quantity, default: 1, null: false
t.timestamps
end
end
add_column :ingredients, :quantity, :integer, :default => 1, :null => false
change_column_null :ingredients, :quantity, false, 1
Loading development environment (Rails 3.2.11)
>> ingredient = Ingredient.new
=> #<Ingredient id: nil, product_id: nil, quantity: nil, unit_id: nil, recipe_id: nil, created_at: nil, updated_at: nil>
>> ingredient.quantity
=> nil
>> ingredient.save
(0.2ms) BEGIN
SQL (10.9ms) INSERT INTO "ingredients" ("created_at", "product_id", "quantity", "recipe_id", "unit_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Thu, 31 Jan 2013 09:35:25 CET +01:00], ["product_id", nil], ["quantity", nil], ["recipe_id", nil], ["unit_id", nil], ["updated_at", Thu, 31 Jan 2013 09:35:25 CET +01:00]]
(1.7ms) COMMIT
=> true
>> ingredient = Ingredient.last
Ingredient Load (1.0ms) SELECT "ingredients".* FROM "ingredients" ORDER BY "ingredients"."id" DESC LIMIT 1
=> #<Ingredient id: 1, product_id: nil, quantity: nil, unit_id: nil, recipe_id: nil, created_at: "2013-01-31 08:35:25", updated_at: "2013-01-31 08:35:25">
>>
@johanb
Copy link
Author

johanb commented Jan 31, 2013

I'm an idiot. rake db:reset does not run migrations, just loads schema.rb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment