Skip to content

Boolean data type in scoped search

July 13, 2011

Boolean type is implemented in different ways by the different database engines, some would represent them as Tiny-Int, Bit or String, some have a natural support for Boolean data type. ActiveRecord supports Boolean data type and will do the relevant translation for us.

For example the following code will create a lights table with ‘Boolean’ column:

class CreateLight < ActiveRecord::Migration
  def self.up
    create_table :lights do |table|
      table.column :color, :string
      table.column :active, :boolean
    end
  end
end

In case we are using MySQL for example, ActiveRecord will create a TINYINT column, in which it will store 0 or 1 and will treats it as a Boolean.

In scoped search the Boolean type is handled by the complete_value  declaration as any other set of values is handled. The complete_value declaration syntax allows  mapping of values and value names. We could, for example use the following syntax:

scoped_search :on => :light, complete_value => {:on => :true, :off => :false}

The above will create a search syntax that allows “light = on” and “light  = off”,  a relevant SQL statement will be generated by the ActiveRecord driver  we are using.

A short syntax for Boolean is also supported by the scoped_search, for example the search term “enabled” will be handled the same as “enabled = true  as long as one of the possible values for enabled is true.

Advertisements

From → Developers

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: