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 |
Add this to your model as an interim step
before_save :set_default_subscribed
def set_default_subscribed
self.subscribed = true if self.subscribed.nil?
end
beware that this won't work
self.subscribed ||= true
@cmaitchison yeah that works nicely. Feels like I'm fighting rails though and life would be a lot easier if I just removed the not null constraint.
Forms don't submit nulls: nulls get stripped from the incoming params by Rails. Assuming HTTP GET/POST and not JSON/XML, that is.
Oh. Wait. Yeah, I think I get it - something else is pulling it from a form params, and it's defaulting to null. Yeah. Workaround, then remove post deploy.
But that's not a bug :p
Shouldn't rails be calling to_bool
when you model.this_attribute_is_a_boolean = ""
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm setting this value from parameters which means that during a rolling deploy, old forms will submit nulls. Also it would be nice if I didn't have to update all the specs, features and fixtures with this parameter,