About ruby on rails, hackintosh, iphone3g and etc.

Wednesday, February 4, 2009

Rails Validate More than 1 Primary Key

In Ruby on Rails, you can specify your model class to validate uniqueness of multiple columns (multiple primary key). Here is the command:

validates_uniqueness of :email, :scope => :name

if you want to validate uniqueness of a triplet or more

validates_uniqueness of :first_attr, :scope => [ :second_attr,
:third_attr ... ]

Monday, February 2, 2009

Connecting Rails to MS SQL Server 2000

The fasted method to connect Rails to MS SQL Server 2000 is using ODBC. Install gem plug-in using the command below:

gem update --system

gem install activerecord-sqlserver-adapter --source=http://gems.rubyonrails.org

Next steps are very important. It is very common for Windows database developer but not if you are from other platform like OSX or Linux.

Create DSN (Data Source Name) in Administrative Tools, Data Sources (ODBC).




Important!!! Make sure the default database is set to the database you wanted to use. Else it will default to Masters database.

Remember the Data Source Name.

Next is to configure your database.yml file in Rails config directory

Here is the settings:

production:
  adapter: sqlserver
  mode: odbc
  dsn: SMSdb
  username: USERNAME
  password: PASSWORD

Try using the rake migration command in my earlier post.

For DSN less configuration (means does not need to create a DSN in Data Sources (ODBC)).

database.yml settings:

development:
    adapter: sqlserver
    mode: odbc
    dsn: DRIVER=/opt/local/lib/libtdsodbc.so;TDS_Version=8.0;SERVER=sqlserver.com;DATABASE=DBNAME;Port=14330;uid=dbusername;pwd=dbpassword;

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