On Thursday, April 12, 2012 12:43:20 AM UTC-7, Gruffalo wrote:
>
> Hello everyone!
>
> I'm learning how Sequel and Ruby work and I wrote this small script which 
> puzzles me.
>
> The logic is:
>
> 1. I create an in-memory database
> 2. I create a table with a boolean field in it
> 3. I declare a model of that table
> 4. I use the model to populate the table
> 5. I try to use .where selector with a boolean field name -- BUT this 
> doesn't work!
>
> The Service.where(:hb_eligible).count returns there, but in fact there are 
> two records in the table that satisfy the criteria.
>
> The introspection shows [:hb_eligible, {:allow_null=>true, :default=>nil, 
> :primary_key=>false, :db_type=>"boolean", :type=>:boolean, 
> :ruby_default=>nil}].
>
> When I do Service.where(:hb_eligible).sql in irb it returns
> => "SELECT * FROM `services` WHERE `hb_eligible`"
> which in my limited understanding of SQL is what I want.
>
> Help?!
>
> Ollie
>
> PS ruby 1.9.3p125 (2012-02-16) [i386-mingw32], sequel 3.34.1
>
> # dbtest.rb
>
> require "rubygems"
> require "bundler/setup"
> require "sequel"
>
> DB = Sequel.sqlite
>
>
Add:

  DB.integer_booleans = true
 
SQLite does not have a true boolean column, and recommends using 0 for 
false and 1 for true.  Sequel by default uses 't' and 'f' on SQLite for 
backwards compatibility.  You can use the integer_booleans setting to 
change things so that 1/0 is used instead of 't'/'f''.  This appears to fix 
your issue.

Just so you know, this:

  Service.where(:hb_eligible)

will not work on all databases.  Some databases (e.g. Microsoft SQL Server) 
do not have boolean columns (recommending that booleans be stored as 
integers), but also do not allow non-boolean conditions in the WHERE 
clause.  Use:

  Service.where(:hb_eligible=>true)

for maximum portability.

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sequel-talk/-/Q_AA-UZxOlYJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to