Multiple select list in rails

I have recently been tinkering with ruby on rails again and was banging my head against the wall with a multiple select list for a “has and belongs to many” relationship between models. After a lot of Googling and experimenting I finally got it running. The resulting code is deceptively simple!

This example illustrates the relationship between users and their roles. Each user can have multiple roles, and each role can have multiple users.

(more…)

 

Date format

Format a date in ruby using the string from time method (strftime).

Format options:
  %a - The abbreviated weekday name (``Sun'')
  %A - The  full  weekday  name (``Sunday'')
  %b - The abbreviated month name (``Jan'')
  %B - The  full  month  name (``January'')
  %c - The preferred local date and time representation
  %d - Day of the month (01..31)
  %H - Hour of the day, 24-hour clock (00..23)
  %I - Hour of the day, 12-hour clock (01..12)
  %j - Day of the year (001..366)
  %m - Month of the year (01..12)
  %M - Minute of the hour (00..59)
  %p - Meridian indicator (``AM''  or  ``PM'')
  %S - Second of the minute (00..60)
  %U - Week  number  of the current year,
          starting with the first Sunday as the first
          day of the first week (00..53)
  %W - Week  number  of the current year,
          starting with the first Monday as the first
          day of the first week (00..53)
  %w - Day of the week (Sunday is 0, 0..6)
  %x - Preferred representation for the date alone, no time
  %X - Preferred representation for the time alone, no date
  %y - Year without a century (00..99)
  %Y - Year with century
  %Z - Time zone name
  %% - Literal ``%'' character
   t = Time.now
   t.strftime("Today is %m/%d/%Y")   #=> "Today is 04/29/2008"
   t.strftime("at %I:%M %p")            #=> "at 10:04 PM"
Tagged with:
 

Ruby on Rails for RHEL 5

Here is the quick and dirty Ruby on Rails setup for Redhat Enterprise Linux v5.0 with MySQL.

(2009-12-04 Update: This does not work with RHEL 5.4. The mysql ruby gem requires ruby 1.8.6 and RHEL 5.4 only ships with ruby 1.8.5)

Using yum install ruby and mysql. The command below does not list all the required packages, but due to dependencies, the additional packages will be installed (or should be – this is all from memory).
(more…)

Tagged with:
 

When displaying a list that requires information from several tables in the database, you can sometimes end up with three or more database queries per list row. Depending on the situation, this may not be a problem. However, it is possible to join the tables in one large SQL query using the :include option.

# :include example
#
@requests = Request.find(:all, :include => [:department, :status])

Using the :include option may improve performance. However, it will likely use more server memory and could potentially return a lot of data through the joins that may not even be used.

Tagged with:
 

Install phpMyAdmin

phpMyAdmin is web-based PHP application for managing MySQL databases. It is fairly easy to install and configure.

Download the latest stable version from the phpMyAdmin web site, and extract it to a location of your choice. I prefer to keep the installation outside of the web root and use an apache alias to reference it.
(more…)

Tagged with: