About ruby on rails, hackintosh, iphone3g and etc.

Wednesday, April 29, 2009

Date Calculation in Rails

>> Date.new( 2008, 1, 1 ).to_time.advance( :months => 1 )
=> Fri Feb 01 00:00:00 +0000 2008


>> Date.new( 2008, 2, 1 ).to_time.advance( :months => 1 )

=> Sat Mar 01 00:00:00 +0000 2008

>> Date.new( 2008, 1, 1 ).to_time + 1.month
=> Thu Jan 31 00:00:00 +0000 2008

Extracted from here:
http://roninonrails.blogspot.com/2008/01/beware-of-rails-date-arithmetic.html

Tuesday, April 28, 2009

Get Month Name in Rails

To get month name as constants, do the following

require 'date'

for i in (1..12)
@month = Date::MONTHNAMES[i]
end

Date Format in strftime

FormatMeaning
%aThe abbreviated weekday name (“Sun'’)
%AThe full weekday name (“Sunday'’)
%bThe abbreviated month name (“Jan'’)
%BThe full month name (“January'’)
%cThe preferred local date and time representation
%dDay of the month (01..31)
%HHour of the day, 24-hour clock (00..23)
%IHour of the day, 12-hour clock (01..12)
%jDay of the year (001..366)
%mMonth of the year (01..12)
%MMinute of the hour (00..59)
%pMeridian indicator (“AM'’ or “PM'’)
%SSecond of the minute (00..60)
%UWeek number of the current year, starting with the first Sunday as the first day of the first week (00..53)
%WWeek number of the current year, starting with the first Monday as the first day of the first week (00..53)
%wDay of the week (Sunday is 0, 0..6)
%xPreferred representation for the date alone, no time
%XPreferred representation for the time alone, no date
%yYear without a century (00..99)
%YYear with century
%ZTime zone name
%%Literal “%'’ character

Saturday, March 7, 2009

Attachment_fu Installation

Installing Attachment_fu plugin steps as follows:

1. Install Freeimage plugin for image processing:

sudo port selfupdate

sudo port install freeimage (link)

MacPorts installation notes can be obtained from here.


2. Install ImageScience

sudo gem install -y image_science


3. Install Attachment_fu


script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/

Wednesday, March 4, 2009

Mongrel_rails

To use mongrel_rails in production, you need to create a service to run in your Windows Server.

Navigate to the directory where rails app are located.

Replace (service name) including bracket with your service name .
Here is the command:

Install service:
mongrel_rails service::install -N (service name) -e (environment. e.g. production) -p (port to be used)

Remove the service:
mongrel_rails service::remove -N (service name)

That's all. Then you can configure lightttpd to point to the mongrel web app.

To start the service from command line:
net start (service name)

Saturday, February 28, 2009

Uninstall SleepWatcher

To unistall SleepWatcher (an useful tool for sleep/wake in OSX)

uninstalling sleepwatcher:

go to terminal
sudo rm /usr/local/sbin/sleepwatcher
sudo rm /usr/local/share/man/man8/sleepwatcher.8
sudo rm /etc/rc.sleep
sudo rm /etc/rc.wakeup

go to finder delete: /Library/StartupItems/SleepWatcher

need your passwd

Thursday, February 26, 2009

SQL SUM() in Rails

In Rails, Active Record support many types of calculations. It saves us from executing SELECT SUM(x) using the query. Everything can be shorten down one line of code.

Example:
To find sum of all items in a location:

Item.sum('location_id', :conditions =>['location_id = ?', location.id])

Similar to avg and others. See ActiveRecord API for more info.