About ruby on rails, hackintosh, iphone3g and etc.

Saturday, January 31, 2009

Uninstalling Xcode

Due to some reasons, you may want to uninstall xcode. Here is the command:

sudo /Developer/Library/uninstall-devtools --mode=all

Friday, January 30, 2009

Starting Rails in Production Environment

Once moving the database to production environment, next is to run rails in production environment. There are a few commands like:

mongrel_rails start -e production

(start mongrel server in production environment)

rake RAILS_ENV=production

Or for console testing:

ruby script/console production

Moving Rails to Production Database

After development (better during development) of rails application, it is good to move/try out the production database environment. To move to production database (e.g. from sqlite to mysql), firstly need to modify the database.yml file in /config directory of rails app.

database.yml

production:
adapter: mysql
database:
username: root
password:
socket: /path/to/your/mysql.sock

Change the database name to your database name. For mysql, create a schema that match the schema name. Enter the username and password, try to avoid root. 

In mysql terminal, create a dababase schema using this command:

mysqladmin -u root -p create

Create a user for this schema and use the username. Leave socket field as it is.

Save database.yml and execute the command below:

rake db:schema:load RAILS_ENV=production

There maybe error if mysql gem plugin is not installed, this can be resolved by this command:

gem install mysql

References:

Thursday, January 22, 2009

Using id after update/create command for rails

In rails, there is a useful method as to capture the id of a record upon successful creation of the record in database.
Example code:

@contact = Contact.new(params[:contact])

respond_to do |format|
if @contact.save

Listing above shows typical execution of creating a row in rails model. Upon save, rails will automatically populate the @contact class with the id generated from database. (@contact.id)

So we can continue to use the following:

@contact = Contact.new(params[:contact])

respond_to do |format|
if @contact.save
#create empty contact details with only contact id
@contact_details = ContactDetails.new()
@contact_details.contact_id = @contact.id
@contact_details.save

@contact.id can be used to populate foreign key of @contact_details.

Friday, January 9, 2009

My Hackintosh Desktop



Configuration:
ThinkPad X61 (Non-Tablet) with 4GB RAM & 2.2GHz Core 2 Duo using iDeneb 10.5.5
Screen Resolution: 1920x1200 with full hardware support of QE/CI




Print Screen Equivalent in OSX (Screen Capture)

In Windows, there is a key to capture screen quickly. Print Screen is the one. Or to capture an individual Window you can use ALT-Print Screen.

In Mac OSX, it is different. There is no such key. Never mind. There is a couple of short cuts which you can use.

1. CMD + SHIFT + 3 - Capture whole screen.
2. CMD + SHIFT + 4 - Gives you a crosshair cursor so you can choose which area of the screen you want to capture then write it to a PNG file on desktop.
3. CMD + CTRL + SHIFT + 4 -gives you a crosshair cursor so you can choose which area of the screen you want to capture and copies it to clipboard. You can paste it into another application like Photoshop and etc.

Have fun.

Thursday, January 8, 2009

vi QuickStart


Starting vi

From the command prompt, type vi and press the enter key. Or, to edit a specific file, type vi, then a space, then the filename, then the enter key.

Examples:
vi Starts vi with no text file name.
vi example.c Starts vi editing the file example.c. If example.c does not exist it will be created when the file is saved.

Quitting vi

From inside vi, type ZZ (shift zz) to save changes and quit. To quit vi without saving the chages type ":q!" without the quotes followed by the enter key.
ZZ save file and quit.

:wq save file and quit.
:q Quit without saving. Gives an error message if the file has been modified since its last save.
:q! Quit without saving and without a warning message that the file was changed.


Editing in vi

There are two modes in vi: command mode and edit mode. vi begins in command mode. To move the curser around use the arrow keys or the h, j, k, and l keys. To enter text from the command mode, first type "i" to insert, which is part of the text entry mode. Then, start typing to enter text before the cursor. Hit the escape key to return to the command mode.

arrow keys move cursor around in vi.
h, j, k, and l keys move cursor around in vi.
i insert at current cursor location.
a insert one character after cursor location (append mode).
A insert at end of current text line.
o creates a new line for editing below current line.
O creates a new line for editing above current line.
x delete character under cursor.
dw delete the characters to the end of the word starting from the current cursor location.
escape key IMPORTANT: returns from edit mode to command mode.
G go to line number specified by number.
G go to last line of program.
1 G go to first line of program.
/string search from current location in file to the end of the file and stop when finding the first occurance of string.
n find the next occurance of the search string specified by the previous "/string" command.
u undo the previous command.
. typing a period repeats the previous command.
:1,$ s/string1/string2/ The first occurance of string1 on each line in the program is replaced with string2.
:1,$ s/string1/string2/g Replaces all occurances of string1 are replaced by string2.