Installing 32 bit and 64 bit versions of Ruby on Mac

Leave a comment

If you have tried installing both 32 bit and 64 bit versions of Ruby together on a Mac, you would have experienced an irritating problem of YAML, shown when bundling:

It seems your ruby installation is missing psych (for YAML output). 
To eliminate this warning, please install libyaml and reinstall your ruby

 

Whatever sequence you follow, this message would come up after installing the 2nd version of ruby.

The solution is simple, though. Install a universal YAML that will work in both 32 bit and 64 bit ruby environments.

To do so:

rvm pkg install --universal libyaml

 

The entire sequence of commands would be something like:

$ rvm get stable
$ rvm pkg install --universal libyaml
$ rvm reload
$ rvm list known
$ rvm_archflags="-arch i386" CONFIGURE_OPTS="--with-arch=i386" 
    CFLAGS="-arch i386" LDFLAGS="-arch i386"
    rvm install ruby-1.9.3-p374 -n i386 --with-arch=i386
$ rvm list
$ rvm install ruby-1.9.3-p374
$ rvm list

Storing Database specific structures as part of Rails Schema

Leave a comment

If you have used an advanced database, like Oracle or PostgreSQL, you might have ended up using some or many of its native object types like Constraints, Triggers, Sequences etc. These native constructs are not recognized by Active Record and consequently not stored as part of schema.rb.

So if you were to create a new database and wanted to migrate the db structures, you will invariably miss out on these custom object types.

But there is a way to tell Rails to use SQL instead of Active Record’s schema dumper. Just uncomment this line in application.rb of your rails application:

config.active_record.schema_format = :sql

 

You can then remove the schema.rb:

rm db/schema.rb

 

Whenever you want to load or dump the schema data, use the following commands instead of schema:

rake db:structure:dump
rake db:structure:load

ActiveRecord outside Rails

Leave a comment

It’s possible to use ActiveRecord to handle or test persistence, outside a Rails project. You can generate models, migrate and write specs just by using the AR gem.

And why would you ever want to use AR outside Rails? In my case, I am writing a gem which needs to parse models to generate schema structures and want to write specs within the gem to test for persistence.

There are blogs and gems dedicated to address this topic like:

The steps are pretty simple:

1. Use ActiveRecord gem

gem.add_dependency  "activerecord", "~> 3.2.0" #if using in a gem
gem "activerecord", "~> 3.2.0" #in Gemfile for all other cases

 

2. Write/Generate a Migration

The simplest migration looks like this:

  class CreatePosts < ActiveRecord::Migration
    def self.up
      create_table :posts, :force => true do |t|
        t.timestamps
      end
    end
    def self.down
      drop_table :yetis
    end
  end

 

You can either hand-code this block (Textmate, for one, has a pretty good Ruby-on-Rails bundle to support this) or you code use a gem like standalone-migrations to generate the model.

3. Establish a database connection

SQLite serves well to create a database on the fly:

ActiveRecord::Base.establish_connection adapter: "sqlite3", database: "db/testdb"

 

You can also create a database.yml and pass it as a parameter to establish_connection. A standard database.yml looks like:

adapter: mysql
database: database
username: user
password: password
host: localhost

 

To load the database.yml:

require 'yaml'
dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)

 

4. Migrate to database

Use ActiveRecord::Migrator to generate the actual table in your database. If your migrations are present in a folder like schema/migrations, then:

ActiveRecord::Migrator.migrate("../schema/migrations")

 

5. Write a Test

describe "Post" do
  it "checks if Post is really an ActiveRecord::Base class" do
    Post.superclass.name.should eq "ActiveRecord::Base"
  end
end

 

This should return a success.

Avoid Active Record and Test Unit Artifacts in your Rails project

Leave a comment

Most of us use RSpec as the default testing framework in rails nowadays. But rails generates a test directory associated with the default Test::Unit framework by default.

To skip generation of Test::Unit artifacts, you can use the –skip-test-unit flag with rails new.

Also, if you were using a NoSQL DB (like MongoDB) and you do not want to use Active Record, you can get rid of ActiveRecord by editing application.rb and development.rb files. An easier approach is to use the –skip-active-record flag during new project generation.

$ rails new app_name --skip-test-unit --skip-active-record

Create .rvmrc for a new Rails project

Leave a comment

After you create a new rails project, it is strongly advised to create a custom gemset for your application. It is also recommended that you create a project rvmrc file, to setup the project’s ruby environment when you switch to the root directory.

You can accomplish both tasks by using ruby –rvmrc –create command. You can use the .rvmrc created to not only protect your gemset, but also to enforce a version of ruby to be used with your project. If the ruby version is not installed, cd‘ing into the directory will download the ruby and install it.

Here are the steps:

1. Create the rails project

$ rails new project_app

2. Change to Project’s root directory:

$ cd project_app

3. Run rvm –rvmrc –create command with the correct ruby_version:

$ rvm --rvmrc --create ruby-1.9.2-p290-i386@project_app

4. Exit out of Project’s root directory:

$ cd ..

5. Change back to Project’s root directory:

$ cd project_app

6. Verify that the new gemset has been chosen:

$ rvm list gemsets

7. Run bundler to install gems in the new gemset:

$ bundle install

Generate Random number in Ruby

Leave a comment

If you want to generate a random number between 1 and 100, just do:

irb> 1+Random.rand(100)

Nginx + Passenger via ruby-build from scratch

Leave a comment

Here’s the steps to install Nginx and Passenger modules via ruby-build, on a Ubuntu 10.04 LTS box.

1. Install Git:

 $ aptitude build-dep git-core
 $ apt-get install git-core

2. Install ruby-build:

 $ git clone git://github.com/sstephenson/ruby-build.git
 $ cd ruby-build
 $ ./install.sh

3. Install Essentials:

 $ for name in {bash,awk,sed,grep,ls,cp,tar,curl,gunzip,bunzip2,git,svn} ; do which $name ; done
 $ apt-get install curl
 $ sudo apt-get install libssl-dev libxslt-dev libxml2-dev
 $ sudo apt-get install libcurl4-openssl-dev
 $ sudo apt-get install libpcre3 libpcre3-dev

4. Install Ruby:

 $ ruby-build 1.9.2-p290 /usr/local/ruby-1.9.2-p290

5. Create ruby_with_env in /usr/bin

 #!/bin/bash
 export LD_LIBRARY_PATH="/opt/oracle/instantclient_10_2:$LD_LIBRARY_PATH"
 export SQLPATH=$LD_LIBRARY_PATH
 export TNS_ADMIN="/opt/oracle/network/admin"
 export NLS_LANG="AMERICAN_AMERICA.UTF8"
 /usr/bin/ruby $*

6. Install Bundler and Passenger

 $ /usr/local/ruby-1.9.2-p290/bin/gem install bundler
 $ /usr/local/ruby-1.9.2-p290/bin/gem install passenger

7. Generate SSH Key (as rails):

 $ ssh-keygen -t rsa -C "example@example.com"
 $ cat id_rsa.pub
 ### Upload key into GitHub
 $ ssh -T git@github.com

8. Deployment Environment setup (from local system):

 $ cap reg_production deploy:setup

9. First Deploy (from local system):

 $ cap reg_production deploy:migrations

10. Install Nginx:

 $ /usr/local/ruby-1.9.2-p290/bin/passenger-install-nginx-module
 ### Choose option 1
 ### Specify correct nginx folder in /usr/local

11. Correct Nginx Conf:

 ### passenger_ruby /usr/local/ruby-1.9.2-p290/bin/ruby_with_env;
 ### include /opt/nginx/conf/sites-enabled/*;

12. Start Ngnix

 $ /usr/local/nginx-ruby-1.9.2-p290/sbin/nginx

Compile schema with dbms_utility.compile_schema

Leave a comment

Just run this statement to compile entire schema:

begin
 dbms_utility.compile_schema('SCOTT');
end;

To be able to execute this, SYS needs the following grants on himself:

grant CREATE ANY DIMENSION to sys;
grant CREATE ANY EVALUATION CONTEXT to sys;
grant CREATE ANY INDEX to sys;
grant CREATE ANY INDEXTYPE to sys;
grant CREATE ANY LIBRARY to sys;
grant CREATE ANY MATERIALIZED VIEW to sys;
grant CREATE ANY OPERATOR to sys;
grant CREATE ANY PROCEDURE to sys;
grant CREATE ANY RULE to sys;
grant CREATE ANY RULE SET to sys;
grant CREATE ANY SYNONYM to sys;
grant CREATE ANY TRIGGER to sys;
grant CREATE ANY TYPE to sys;
grant CREATE ANY VIEW to sys;

Enabling Shared folders on Ubuntu Guest in Parallels

Leave a comment

You will need to install Parallels Tools before you can access Shared Folders.

Info on installing Parallels Tools is here: http://download.parallels.com/desktop/v6/docs/en/Parallels_Desktop_Users_Guide/22570.htm

Permissions/Settings for Oracle XE on Ubuntu

Leave a comment

Great post explaining all settings to be tweaked for using Oracle XE database, for access/control from a user environment:

http://ubuntuforums.org/showpost.php?p=7838671&postcount=4

Older Entries