public
Rubygem
Description: Generated scopes for ActiveRecord classes
Clone URL: git://github.com/thoughtbot/pacecar.git
dont create null not null checks for boolean columns, because thats INSANE 
and defies logic
mjankowski (author)
Wed Oct 08 12:46:25 -0700 2008
commit  49aca2009a077a9051ab189327482e1a27b67a22
tree    1019c33ca6f2b6648cd679fee8796ef37735cbeb
parent  e909de91e898edfdc288013592fa296ff5993602
...
55
56
57
58
59
60
61
62
63
64
65
...
55
56
57
 
 
 
 
 
58
59
60
0
@@ -55,11 +55,6 @@ And some basic model declarations...
0
 
0
 = All columns
0
 
0
-Records where admin is not null, or where it is null...
0
-
0
- User.admin_present
0
- User.admin_missing
0
-
0
 Records where approved_at is not null, or where it is null...
0
 
0
   User.approved_at_present
...
20
21
22
 
 
 
 
23
24
25
...
20
21
22
23
24
25
26
27
28
29
0
@@ -20,6 +20,10 @@ module Pacecar
0
         columns.select { |column| types.include? column.type }.collect(&:name)
0
       end
0
 
0
+ def column_names_without_type(*types)
0
+ columns.select { |column| ! types.include? column.type }.collect(&:name)
0
+ end
0
+
0
       def boolean_column_names
0
         column_names_for_type :boolean
0
       end
...
12
13
14
15
 
16
17
18
...
12
13
14
 
15
16
17
18
0
@@ -12,7 +12,7 @@ module Pacecar
0
       protected
0
 
0
       def define_presence_scopes
0
- column_names.each do |name|
0
+ column_names_without_type(:boolean).each do |name|
0
           named_scope "#{name}_present".to_sym, :conditions => "#{quoted_table_name}.#{name} is not null"
0
           named_scope "#{name}_missing".to_sym, :conditions => "#{quoted_table_name}.#{name} is null"
0
         end
...
17
18
19
 
 
 
 
20
21
22
...
17
18
19
20
21
22
23
24
25
26
0
@@ -17,6 +17,10 @@ class PresenceTest < Test::Unit::TestCase
0
         proxy_options = { :conditions => '"users".first_name is null' }
0
         assert_equal proxy_options, @class.first_name_missing.proxy_options
0
       end
0
+ should "not setup methods for boolean columns" do
0
+ assert ! @class.respond_to?(:admin_missing)
0
+ assert ! @class.respond_to?(:admin_present)
0
+ end
0
     end
0
   end
0
 

Comments

  • Sadly SQL Server allows null on boolean’s. Insane? YES!

  • Yeah, so does mysql … but I don’t want to reward bad behavior by putting those methods out there. I think it’s best practice to put a not null constraint on rails boolean/tinyint columns, and default them to false.

  • Agreed, I’ve been in the habit of always forcing a :default and in the case of SQL Server doing an update all after adding said boolean to a table since it will leave all the existing records at NULL after the mig if I do not. Good work on the plugin too. I’m about to fork it and see if I can start to add some granular class declarations that generates batches of named_scopes vs the en-masse that is happening now. Cheers!