Last active
December 14, 2015 15:08
-
-
Save compactcode/5105152 to your computer and use it in GitHub Desktop.
Rails fun with a not null constraint and a default value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CreateCustomers < ActiveRecord::Migration | |
| def change | |
| create_table :customers do |t| | |
| t.string :name, :null => false | |
| t.boolean :subscribed, :null => false, :default => true | |
| t.timestamps | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| curl --data "customer[name]=shanon" http://localhost:3000/customers.json => 200 | |
| curl --data "customer[name]=shanon&customer[subscribed]=0" http://localhost:3000/customers.json => 200 | |
| curl --data "customer[name]=shanon&customer[subscribed]=" http://localhost:3000/customers.json => 500 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Customer.create!(:name => "shanon") => #<Customer id: 1 ... > | |
| Customer.create!(:name => "shanon", :subscribed => nil) => ActiveRecord::StatementInvalid | |
| Customer.create!(:name => "shanon", :subscribed => "") => ActiveRecord::StatementInvalid |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Customer < ActiveRecord::Base | |
| attr_accessible :name, :subscribed | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CustomersController < ApplicationController | |
| respond_to :json | |
| def create | |
| respond_with Customer.create(params[:customer]) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't rails be calling
to_boolwhen youmodel.this_attribute_is_a_boolean = ""?