<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rubyonrails code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 08:26:06 GMT</pubDate>
    <description>DZone Snippets: rubyonrails code</description>
    <item>
      <title>Custom Rails Callback</title>
      <link>http://snippets.dzone.com/posts/show/5226</link>
      <description>// This shows you how to extend active record to allow a custom callback.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#ever needed to add your own callback into the active record callback chain?&lt;br /&gt;#here's how...&lt;br /&gt;module ActiveRecord&lt;br /&gt;  &lt;br /&gt;  #to keep things clean we'll define our own module&lt;br /&gt;  module SweetCustomCallbacks&lt;br /&gt;    &lt;br /&gt;    #this is the ruby method that is called when you call include on a module&lt;br /&gt;    #the method passed, base in our case, is the class your including yourself in&lt;br /&gt;    def self.included(base)&lt;br /&gt;      &lt;br /&gt;      #this clas_eval method takes a block that will be added to base, which is ActiveRecord::Base&lt;br /&gt;      #basically this is like writing code directly into the class ActiveRecord::Base&lt;br /&gt;      base.class_eval do&lt;br /&gt;        &lt;br /&gt;        #alias_method_chain is very cool&lt;br /&gt;        #it allows us override a method without removing the original and without the original knowing its been changed&lt;br /&gt;        #here's how it works:&lt;br /&gt;        # 1st argument is the name of the method you're overriding&lt;br /&gt;        # 2nd argument is feature you want to add&lt;br /&gt;        # so if create_or_update is called it will actually call create_or_update_with_sweet_custom_callbacks&lt;br /&gt;        # the original is still avaliable at create_or_update_without_custom_callbacks&lt;br /&gt;        &lt;br /&gt;        alias_method_chain :create_or_update, :sweet_custom_callbacks&lt;br /&gt;        #the method create_or_update is an internals method to ActiveRecord that is called whenever you call .save or .update&lt;br /&gt;        &lt;br /&gt;        #this is the class method that allows the cool syntax:&lt;br /&gt;        # class Article &lt; ActiveRecord::Base&lt;br /&gt;        # before_before_save :add_authors_name&lt;br /&gt;        #private&lt;br /&gt;        #def add_authors_name&lt;br /&gt;        #.....&lt;br /&gt;        def self.before_before_save(*callbacks, &amp;block)&lt;br /&gt;          callbacks &lt;&lt; block if block_given? #add the block to the array of callback functions if a block was passed&lt;br /&gt;          write_inheritable_array(:before_before_save,callbacks) &lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    #the instance method, although not recomended is avaliable for overriding&lt;br /&gt;    #also if you wanted to have a callback that always did something say add the users's id to the object &lt;br /&gt;    #you could always code that in here and remove the class above&lt;br /&gt;    def before_before_save() end&lt;br /&gt;&lt;br /&gt;    #ok this is the method that is now called instead of create_or_update&lt;br /&gt;    #it calls a method callback on &lt;br /&gt;    def create_or_update_with_sweet_custom_callbacks&lt;br /&gt;      return false if callback(:before_before_save) == false  #this method does all the real work and calls the callback function in callbacks.rb that gets the methods and runs them &lt;br /&gt;      create_or_update_without_sweet_custom_callbacks&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;   end&lt;br /&gt; &lt;br /&gt;   class Base    &lt;br /&gt;     include SweetCustomCallbacks #include our module in ActiveRecord::Base&lt;br /&gt;   end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 13 Mar 2008 16:28:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5226</guid>
      <author>onkis (Mike Ball)</author>
    </item>
    <item>
      <title>Convert a UTF-8 string to ISO-8859-1</title>
      <link>http://snippets.dzone.com/posts/show/5019</link>
      <description>Convert a utf string to iso, used this when generating a pdf with pdf-writer in Rails, all my text is UTF8 but pdf-writer does not support this.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#add this to environment.rb&lt;br /&gt;#call to_iso on any UTF8 string to get a ISO string back&lt;br /&gt;#example : "C&#233;dez le passage aux fran&#231;ais".to_iso&lt;br /&gt;&lt;br /&gt;class String&lt;br /&gt;  require 'iconv' #this line is not needed in rails !&lt;br /&gt;  def to_iso&lt;br /&gt;    Iconv.conv('ISO-8859-1', 'utf-8', self)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 21 Jan 2008 14:35:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5019</guid>
      <author>drcorbeille (Christian Meichtry)</author>
    </item>
    <item>
      <title>open_body_tag</title>
      <link>http://snippets.dzone.com/posts/show/4726</link>
      <description>&lt;code&gt;&lt;br /&gt;  #creates a body tag uniquely indentifying this page&lt;br /&gt;  #takes an options Hash with two keys:&lt;br /&gt;  #&lt;br /&gt;  #&lt;tt&gt;id&lt;/tt&gt;::        string that will be used as the body's ID. defaults to &lt;tt&gt;controller.controller_name.singularize&lt;/tt&gt;&lt;br /&gt;  #&lt;tt&gt;classes&lt;/tt&gt;::   an Array of class names. defaults to &lt;tt&gt;[params[:action]]&lt;/tt&gt;&lt;br /&gt;  #&lt;br /&gt;  #Examples:&lt;br /&gt;  #&lt;br /&gt;  # in HomeController#index:&lt;br /&gt;  #&lt;br /&gt;  # &lt;%= open_body_tag %&gt;&lt;br /&gt;  # =&gt; &lt;body id='home' class='index'&gt;&lt;br /&gt;  #&lt;br /&gt;  # &lt;%= open_body_tag(:id =&gt; 'foo') %&gt;&lt;br /&gt;  # =&gt; &lt;body id='foo' class='index'&gt;&lt;br /&gt;  #&lt;br /&gt;  # &lt;%= open_body_tag(:id =&gt; 'foo', :classes =&gt; %w(one two)) %&gt;&lt;br /&gt;  # =&gt; &lt;body id='foo' class='one two'&gt;&lt;br /&gt;  def open_body_tag(options = { :id =&gt; controller.controller_name.singularize, :classes =&gt; [params[:action]] })&lt;br /&gt;    "&lt;body id='#{options[:id]}' class='#{options[:classes].join(' ')}'&gt;"&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 01 Nov 2007 19:24:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4726</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Convert .rhtml templates to .html.erb</title>
      <link>http://snippets.dzone.com/posts/show/4640</link>
      <description>// run this shell command from within the dir you want to change. You will need to change the hg line to use which ever version control you are using, or if you're not use version control (naughty!) then leave out the hg letters altogether.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;for f in $(find . -name '*.rhtml') ; do c=$(dirname $f)/$(basename $f .rhtml).html.erb ; hg mv $f $c ; done&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 12 Oct 2007 08:58:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4640</guid>
      <author>whomwah (Duncan Robertson)</author>
    </item>
    <item>
      <title>Rails URL Validation</title>
      <link>http://snippets.dzone.com/posts/show/4532</link>
      <description>No regexes, allows URLs with ports or IPs. Inspiration from &lt;a href="http://actsasblog.wordpress.com/2006/10/16/url-validation-in-rubyrails/"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  validates_each :href, :on =&gt; :create do |record, attr, value|&lt;br /&gt;    begin&lt;br /&gt;      uri = URI.parse(value)&lt;br /&gt;      if uri.class != URI::HTTP&lt;br /&gt;        record.errors.add(attr, 'Only HTTP protocol addresses can be used')&lt;br /&gt;      end&lt;br /&gt;    rescue URI::InvalidURIError&lt;br /&gt;      record.errors.add(attr, 'The format of the url is not valid.')&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Sep 2007 14:03:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4532</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Helper for testing default routes generated by a resource in Ruby on Rails</title>
      <link>http://snippets.dzone.com/posts/show/4517</link>
      <description>These methods test that the routes for resources defined in routes.rb are working as expected. Call them from your functional (controller) tests.&lt;br /&gt;&lt;br /&gt;Add the following 3 methods to test/test_helper.rb (updated for Rails 1.2.5 which no longer uses semicolons as a separator for the edit action):&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Test for routes generated by map.resource (singular).&lt;br /&gt;def assert_routing_for_resource(controller, skip=[], nesting=[])&lt;br /&gt;  routes = [&lt;br /&gt;    ["new",'/new',{},:get], ["create",'',{},:post],&lt;br /&gt;    ["show",'',{},:get], ["edit",'/edit',{},:get],&lt;br /&gt;    ["update",'',{},:put], ["destroy",'',{},:delete]&lt;br /&gt;    ]&lt;br /&gt;  check_resource_routing(controller, routes, skip, nesting)&lt;br /&gt;end&lt;br /&gt;# Test for routes generated by map.resources (plural).&lt;br /&gt;def assert_routing_for_resources(controller, skip=[], nesting=[])&lt;br /&gt;  routes = [&lt;br /&gt;    ["index",'',{},:get], ["new",'/new',{},:get], ["create",'',{},:post],&lt;br /&gt;    ["show",'/1',{:id=&gt;'1'},:get], ["edit",'/1/edit',{:id=&gt;'1'},:get],&lt;br /&gt;    ["update",'/1',{:id=&gt;'1'},:put], ["destroy",'/1',{:id=&gt;'1'},:delete]&lt;br /&gt;    ]&lt;br /&gt;  check_resource_routing(controller, routes, skip, nesting)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Check that the expected paths will be generated by a resource, and that&lt;br /&gt;# the expected params will be generated by paths defined by a resource.&lt;br /&gt;# routes is array of [action, url string after controller, extra params].&lt;br /&gt;def check_resource_routing(controller, routes, skip=[], nesting=[])&lt;br /&gt;  # set a prefix for nested resources&lt;br /&gt;  prefix = nesting.join('s/1/')&lt;br /&gt;  unless prefix.blank?&lt;br /&gt;    prefix += "s/1/"&lt;br /&gt;  end&lt;br /&gt;  # Add params for nested resources.&lt;br /&gt;  # For each 'nest', include a ":nest_id=&gt;'1'" param.&lt;br /&gt;  params = {}&lt;br /&gt;  nesting.each do |param|&lt;br /&gt;    params["#{param}_id".to_sym] = '1'&lt;br /&gt;  end&lt;br /&gt;  # Test each of the standard resource routes.&lt;br /&gt;  routes.each do |pair|&lt;br /&gt;    unless skip.include? pair[0]&lt;br /&gt;      assert_generates("/#{prefix}#{controller}#{pair[1]}",&lt;br /&gt;        {:controller=&gt;controller,&lt;br /&gt;        :action=&gt;pair[0]}.merge(pair[2]).merge(params), {}, {},&lt;br /&gt;        "Failed generation of resource route for action #{pair[0]} /#{prefix}#{controller}#{pair[1]}")&lt;br /&gt;      assert_recognizes(&lt;br /&gt;        {:controller=&gt;controller,&lt;br /&gt;          :action=&gt;pair[0]}.merge(pair[2]).merge(params),&lt;br /&gt;        {:path=&gt;"/#{prefix}#{controller}#{pair[1]}", :method=&gt;pair[3]},&lt;br /&gt;        {}, "Failed to recognize resource route for path #{pair[3]}:/#{prefix}#{controller}#{pair[1]}")&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;EXAMPLES&lt;br /&gt;&lt;br /&gt;You can specify actions to 'skip' (if you have a special route for that action).&lt;br /&gt;If using nested resources, set the nesting array (use singular strings).&lt;br /&gt;&lt;br /&gt;So, if you have the following in routes.rb:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;map.make_thing '/make', :controller=&gt;'things', :action=&gt;'new'&lt;br /&gt;map.resources :nests do |nest|&lt;br /&gt;  nest.resources :things&lt;br /&gt;end&lt;br /&gt;map.resource :foo&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;then you can use the following in things_controller_test.rb:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def test_resource_routing&lt;br /&gt;  assert_routing_for_resources 'things', ['new'], ['nest']&lt;br /&gt;  assert_routing_for_resource 'foo'&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 10 Sep 2007 12:46:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4517</guid>
      <author>grantneufeld (Grant Neufeld)</author>
    </item>
    <item>
      <title>Modified Growl support for Ruby autotest</title>
      <link>http://snippets.dzone.com/posts/show/4514</link>
      <description># This code is a different approach to using Growl notifications (Mac OS X)&lt;br /&gt;# with autotest for Ruby.&lt;br /&gt;# The code can be added to ~/.autotest&lt;br /&gt;# Be sure to comment out "require 'autotest/growl'" if you have that line&lt;br /&gt;# in your .autotest file.&lt;br /&gt;# Based on ZenTest-3.6.1/lib/autotest/growl.rb&lt;br /&gt;# Requires the 2 rails png graphics found at:&lt;br /&gt;# http://blog.internautdesign.com/2006/11/12/autotest-growl-goodness&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module Autotest::Growl&lt;br /&gt;  # save the name of the working directory (e.g., the app name)&lt;br /&gt;  # to be used as a key to identify this particular run of autotest&lt;br /&gt;  # so the same growl notification will be reused for updates,&lt;br /&gt;  # instead of a new growl window for every posting.&lt;br /&gt;  @@current_directory_name = Dir.pwd.match(/([^\/]+)\z/).to_s&lt;br /&gt;  def self.growl title, msg, pri=0&lt;br /&gt;    title += " in #{@@current_directory_name}"&lt;br /&gt;    msg += " at #{Time.now}"&lt;br /&gt;    # ### change the path to the directory for the png graphics:&lt;br /&gt;    system "growlnotify -n autotest #{pri &gt; 0 ? '-s ' : ''}--image /Users/grant/Pictures/rails_#{pri &gt; 0 ? 'fail' : 'ok'}.png -p #{pri} -d '#{Dir.pwd}' -m #{msg.inspect} #{title}"&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  Autotest.add_hook :run do  |at|&lt;br /&gt;    growl "autotest running", "Started"&lt;br /&gt;  end&lt;br /&gt;  Autotest.add_hook :red do |at|&lt;br /&gt;    growl "Tests Failed", "#{at.files_to_test.size} tests failed", 2&lt;br /&gt;  end&lt;br /&gt;  Autotest.add_hook :green do |at|&lt;br /&gt;    growl "Tests Passed", "Tests passed", -2 if at.tainted&lt;br /&gt;  end&lt;br /&gt;  Autotest.add_hook :all_good do |at|&lt;br /&gt;    growl "Tests Passed", "All tests passed", -2 if at.tainted&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 09 Sep 2007 13:22:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4514</guid>
      <author>grantneufeld (Grant Neufeld)</author>
    </item>
    <item>
      <title>Using POP3 to Retrieve Email for Rails App</title>
      <link>http://snippets.dzone.com/posts/show/4214</link>
      <description>Put this in a file called "inbox" in the scripts/ directory of your Rails app. change the host, username, and password to match.&lt;br /&gt;&lt;br /&gt;Set up a cron job to run this script every so often (frequency depends on your needs).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;&lt;br /&gt;require 'net/pop'&lt;br /&gt;require File.dirname(__FILE__) + '/../config/environment'&lt;br /&gt;&lt;br /&gt;logger = RAILS_DEFAULT_LOGGER&lt;br /&gt;&lt;br /&gt;logger.info "Running Mail Importer..." &lt;br /&gt;Net::POP3.start("localhost", nil, "username", "password") do |pop|&lt;br /&gt;  if pop.mails.empty?&lt;br /&gt;    logger.info "NO MAIL" &lt;br /&gt;  else&lt;br /&gt;    pop.mails.each do |email|&lt;br /&gt;      begin&lt;br /&gt;        logger.info "receiving mail..." &lt;br /&gt;        Notifier.receive(email.pop)&lt;br /&gt;        email.delete&lt;br /&gt;      rescue Exception =&gt; e&lt;br /&gt;        logger.error "Error receiving email at " + Time.now.to_s + "::: " + e.message&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;logger.info "Finished Mail Importer." &lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 26 Jun 2007 17:40:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4214</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Setting up Ruby On Rails with PostgreSQL on Mac OS X 10.4.9</title>
      <link>http://snippets.dzone.com/posts/show/4187</link>
      <description>This is a modified version of the &lt;a href="http://wiki.rubyonrails.org/rails/pages/Tutorial"&gt;Ruby on Rails Tutorial&lt;/a&gt; (&lt;a href="http://wiki.rubyonrails.org/rails/pages/TutorialStepOne"&gt;TutorialStepOne&lt;/a&gt;, &lt;a href="http://wiki.rubyonrails.org/rails/pages/TutorialStepOnePostgresql"&gt;TutorialStepOnePostgresql&lt;/a&gt;, ...).&lt;br /&gt;&lt;br /&gt;Log in to an admin user account and, if necessary, fix your command search path in $HOME/.bash_profile, $HOME/.bash_login or $HOME/.profile to include "/usr/local" and "/usr/local/sbin" (cf. &lt;a href="http://hivelogic.com/narrative/articles/using_usr_local"&gt;Using /usr/local &gt; Set The Path&lt;/a&gt;; echo $PATH | tr ":" "\n"). If there are no such files, just create them: touch $HOME/.bash_login &amp;&amp; touch $HOME/.bashrc (ls -a | grep \.bash).&lt;br /&gt;&lt;br /&gt;To avoid RubyGems loading issues it's no bad idea to add  &lt;a href="http://rubygems.org/read/chapter/3"&gt;export RUBYOPT=rubygems&lt;/a&gt;  to your $HOME/.bash_login file as well.&lt;br /&gt;&lt;br /&gt;If you want your system path changes to take effect not only for you you can modify the global system path settings in the systemwide initialization files /private/etc/profile and /private/etc/bashrc accordingly.&lt;br /&gt;&lt;br /&gt;To fix the paths of installed manual pages add the lines "MANPATH /usr/local/share/man" and "MANPATH /usr/local/man" to sudo nano +45 /usr/share/misc/man.conf (man -w | tr ":" "\n").&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(USE THE FOLLOWING AT YOUR OWN RISK!)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;REQUIREMENTS:&lt;br /&gt;&lt;br /&gt;I.     &lt;a href="http://developer.apple.com/tools/xcode/index.html"&gt;Xcode&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;II.    PostgreSQL Database Server&lt;br /&gt;&lt;br /&gt;Install this &lt;a href="http://www.entropy.ch/software/macosx/postgresql/"&gt;PostgreSQL Database Server&lt;/a&gt; package.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# test after installation&lt;br /&gt;which psql     # /usr/local/bin/psql&lt;br /&gt;psql --version     # psql (PostgreSQL) 8.2.3, contains support for command-line editing&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Alternative installation: &lt;a href="http://brilliantcorners.org/node/84"&gt;Getting PostgreSQL running for Rails on a Mac&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;III.   Ruby 1.8.6, Ruby On Rails 1.2.3 &amp; Mongrel 1.0.1&lt;br /&gt;&lt;br /&gt;&lt;a href="http://hivelogic.com/narrative/articles/ruby-rails-mongrel-mysql-osx"&gt;Building Ruby, Rails, Subversion, Mongrel, and MySQL on Mac OS X&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;To install both Mongrel &amp; Mongrel Cluster use: sudo gem install -y mongrel mongrel_cluster (cf. &lt;a href="http://mongrel.rubyforge.org/docs/mongrel_cluster.html"&gt;Using Mongrel Cluster&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# test after installation&lt;br /&gt;ruby -v     # ruby 1.8.6&lt;br /&gt;rails -v     # Rails 1.2.3&lt;br /&gt;gem list     # ... mongrel (1.0.1) ...&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;IV. &lt;a href="http://rubyforge.org/projects/ruby-postgres/"&gt;ruby-postgres&lt;/a&gt; 0.7.1&lt;br /&gt;&lt;code&gt;&lt;br /&gt;sudo gem install -y ruby-postgres&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As an alternative you may try &lt;a href="http://www.dotrb.com/2007/3/25/ruby-on-rails-installer-script-for-mac-osx"&gt;Ruby on rails installer script for Mac OSX&lt;/a&gt; or choose to install via MacPorts as described in &lt;a href="http://www.robbyonrails.com/articles/2007/06/19/installing-ruby-on-rails-and-postgresql-on-os-x-second-edition"&gt;Installing Ruby on Rails and PostgreSQL on OS X, Second Edition&lt;/a&gt; or &lt;a href="http://blog.nanorails.com/articles/2006/10/17/installing-rails-on-mac-os-x-tiger-10-4-8"&gt;Installing Rails on Mac OS X Tiger (10.4.8)&lt;/a&gt;. However, you then may have to change your system paths mentioned above &lt;a href="http://www.railsontherun.com/2007/5/9/installing-postgresql-on-mac"&gt;accordingly&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;1. create a database server&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;# first create a directory called PostgreSQL-db on your Desktop&lt;br /&gt;mkdir -p $HOME/Desktop/PostgreSQL-db&lt;br /&gt;&lt;br /&gt;# create a new db server called railsdb&lt;br /&gt;/usr/local/bin/initdb -E UTF8 -D $HOME/Desktop/PostgreSQL-db/railsdb&lt;br /&gt;&lt;br /&gt;# START DB SERVER&lt;br /&gt;dir="$HOME/Desktop/PostgreSQL-db"; /usr/local/bin/pg_ctl -D $dir/railsdb -l $dir/railsdb/postgres.log start&lt;br /&gt;&lt;br /&gt;# STOP DB SERVER&lt;br /&gt;#dir="$HOME/Desktop/PostgreSQL-db"; /usr/local/bin/pg_ctl -D $dir/railsdb -l $dir/railsdb/postgres.log stop -m smart&lt;br /&gt;&lt;br /&gt;# check&lt;br /&gt;cat $HOME/Desktop/PostgreSQL-db/railsdb/pg_hba.conf&lt;br /&gt;&lt;br /&gt;   ...&lt;br /&gt;   # TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD&lt;br /&gt;&lt;br /&gt;   # "local" is for Unix domain socket connections only&lt;br /&gt;   local   all         all                               trust&lt;br /&gt;   # IPv4 local connections:&lt;br /&gt;   host    all         all         127.0.0.1/32          trust&lt;br /&gt;   # IPv6 local connections:&lt;br /&gt;   host    all         all         ::1/128               trust&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;2. create PostgreSQL database&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;createdb `whoami`_development&lt;br /&gt;#dropdb `whoami`_development&lt;br /&gt;&lt;br /&gt;createdb `whoami`_test&lt;br /&gt;#dropdb `whoami`_test&lt;br /&gt;&lt;br /&gt;#createdb `whoami`_production&lt;br /&gt;#dropdb `whoami`_production&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;3. set up your Rails project&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;cd $HOME/Desktop/RubyOnRails-projects&lt;br /&gt;rails -d postgresql `whoami`&lt;br /&gt;cd `whoami`&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;4. edit /config/database.yml&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;open -e $HOME/Desktop/RubyOnRails-projects/`whoami`/config/database.yml&lt;br /&gt;&lt;br /&gt;# just uncomment the following line&lt;br /&gt;#encoding: UTF8 &lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;5. make Rails &amp; PostgreSQL work together&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;cd $HOME/Desktop/RubyOnRails-projects/`whoami`&lt;br /&gt;&lt;br /&gt;ruby script/generate migration People&lt;br /&gt;&lt;br /&gt;# edit /db/migrate/001_people.rb&lt;br /&gt;# cf. http://wiki.rubyonrails.org/rails/pages/TutorialStepOneMigrations&lt;br /&gt;&lt;br /&gt;open -e $HOME/Desktop/RubyOnRails-projects/`whoami`/db/migrate/001_people.rb&lt;br /&gt;&lt;br /&gt; class People &lt; ActiveRecord::Migration&lt;br /&gt;&lt;br /&gt;    def self.up&lt;br /&gt;      create_table :people do |table|&lt;br /&gt;      # note that "id" is added implicitly, by default&lt;br /&gt;        table.column :name, :string&lt;br /&gt;        table.column :street1, :string&lt;br /&gt;        table.column :street2, :string&lt;br /&gt;        table.column :city, :string&lt;br /&gt;        table.column :state, :string&lt;br /&gt;        table.column :zip, :string&lt;br /&gt;     end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def self.down&lt;br /&gt;      drop_table :people &lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;rake db:migrate&lt;br /&gt;&lt;br /&gt;ruby script/generate model Person  &lt;br /&gt;&lt;br /&gt;#open -e $HOME/Desktop/RubyOnRails-projects/`whoami`/app/models/person.rb  # file will be explained below&lt;br /&gt;&lt;br /&gt;ruby script/console&lt;br /&gt;&lt;br /&gt;&gt;&gt; ...&lt;br /&gt;        entry = Person.new&lt;br /&gt;        entry.name = "Name" &lt;br /&gt;        entry.street1 = "123 Somwhere" &lt;br /&gt;        entry.street2 = "" &lt;br /&gt;        entry.city = "Smallville" &lt;br /&gt;        entry.state = "KS" &lt;br /&gt;        entry.zip = "123456" &lt;br /&gt;        entry.save&lt;br /&gt;        exit&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# check newly created db table&lt;br /&gt;&lt;br /&gt;psql `whoami`_development&lt;br /&gt;SELECT * FROM people;&lt;br /&gt;\q&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# test&lt;br /&gt;rake      # ... 0 failures, 0 errors&lt;br /&gt;&lt;br /&gt;# create new controller&lt;br /&gt;ruby script/generate controller People list view new edit&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# edit /app/controllers/people_controller.rb&lt;br /&gt;open -e $HOME/Desktop/RubyOnRails-projects/`whoami`/app/controllers/people_controller.rb&lt;br /&gt;&lt;br /&gt;  def view&lt;br /&gt;   @person = Person.find(1)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# edit /app/views/people/view.rhtml&lt;br /&gt;open -e $HOME/Desktop/RubyOnRails-projects/`whoami`/app/views/people/view.rhtml&lt;br /&gt;&lt;br /&gt;# copy &amp; paste &amp; uncomment the following lines&lt;br /&gt;&lt;br /&gt;#&lt;html&gt;&lt;br /&gt;#  &lt;body&gt;&lt;br /&gt;#    &lt;h1&gt;People#view&lt;/h1&gt;&lt;br /&gt;#    &lt;p&gt;This page will display one person.&lt;/p&gt;&lt;br /&gt;#    &lt;p&gt;&lt;br /&gt;#    &lt;%= @person.name %&gt;&lt;br /&gt;&lt;br /&gt;#    &lt;%= @person.street1 %&gt;&lt;br /&gt;&lt;br /&gt;#    &lt;%= @person.street2 %&gt;&lt;br /&gt;&lt;br /&gt;#    &lt;%= @person.city %&gt;&lt;br /&gt;&lt;br /&gt;#    &lt;%= @person.state %&gt;&lt;br /&gt;&lt;br /&gt;#    &lt;%= @person.zip %&gt;&lt;br /&gt;&lt;br /&gt;#    &lt;/p&gt;&lt;br /&gt;#  &lt;/body&gt;&lt;br /&gt;#&lt;/html&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# the file /app/models/person.rb explained (see above)&lt;br /&gt;# open -e $HOME/Desktop/RubyOnRails-projects/`whoami`/app/models/person.rb&lt;br /&gt;&lt;br /&gt;# class Person &lt; ActiveRecord::Base&lt;br /&gt;# end&lt;br /&gt;&lt;br /&gt;# How does this know to map to the people table we created? ActiveRecord pluralizes the class name and looks for that &lt;br /&gt;# table in the database. This doesn&#8217;t just mean adding an &#8217;s&#8217;. Irregular plural forms are also handled, so Rails knows &lt;br /&gt;# that the plural of &#8216;person&#8217; is &#8216;people&#8217;. The rules for how it does this are described in the documentation&lt;br /&gt;# (see http://wiki.rubyonrails.org/rails/pages/TutorialStepSix).&lt;br /&gt;&lt;br /&gt;# test&lt;br /&gt;rake&lt;br /&gt;&lt;br /&gt;# start your Rails app&lt;br /&gt;cd $HOME/Desktop/RubyOnRails-projects/`whoami`&lt;br /&gt;&lt;br /&gt;ruby script/server&lt;br /&gt;#ruby script/server --environment=development&lt;br /&gt;&lt;br /&gt;# open a second shell window and go to ...  &lt;br /&gt;open -a Safari http://localhost:3000/people/view&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 21 Jun 2007 21:25:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4187</guid>
      <author>ntk ()</author>
    </item>
    <item>
      <title>Print and preselect checkboxes</title>
      <link>http://snippets.dzone.com/posts/show/4101</link>
      <description>Create a selection box list and preselect with previously assigned Roles&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def createCheckBoxList(user)&lt;br /&gt;    &lt;br /&gt;    userRolesID = Hash.new&lt;br /&gt;    user.roles.each do | ur |&lt;br /&gt;      userRolesID[ur.name] = ur.id   &lt;br /&gt;    end&lt;br /&gt; &lt;br /&gt;    @roles = Role.find(:all)&lt;br /&gt;    &lt;br /&gt;    html_output = String.new&lt;br /&gt;    # first create the checkbox&lt;br /&gt;      for role in @roles&lt;br /&gt;        if userRolesID.has_key?(role.name)&lt;br /&gt;          html_output &lt;&lt; check_box("roles_user", role.id, :checked =&gt; 'checked')&lt;br /&gt;        else&lt;br /&gt;          html_output &lt;&lt; check_box("roles_user", role.id)&lt;br /&gt;        end&lt;br /&gt;        # then print the role name&lt;br /&gt;        html_output &lt;&lt; " " + role.name + "&lt;br /&gt;"&lt;br /&gt;      end&lt;br /&gt;      &lt;br /&gt;      return html_output&lt;br /&gt;    &lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 03 Jun 2007 21:45:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4101</guid>
      <author>vkeesari (VK)</author>
    </item>
  </channel>
</rss>
