Ruby on Rails Development
Sunday, May 21, 2006
Working with Boolean Fields
Creation:
Active Record migrations are quite good at creating tables. The below migration will add table "table" to the db with a boolean field a_boolean when rake db:migrate is run.
class CreateTable < ActiveRecord::Migration def self.up create_table :table do |t| t.column :a_boolean, :boolean end end
def self.down drop_table :table end end
Boolean Form Field Select Helper:
The following line creates a select box with true and false represented by Yes and No. The currently selected value (while editing) is automatically selected.
<%= select('table', 'a_boolean', [["Yes",true],["No",false]]) %>
Validation:
validates_each :a_boolean do |record, attr, value| record.errors.add attr, 'is invalid.' if value != true && value != false end
2:26 PM posted by J. Michael Cunningham
|