Archive for the ‘Languages’ Category
Friday, August 22nd, 2008
Eclipse 3.4 (Ganymede) was released a few months ago.
I have been working on the studio project on my laptop, using Eclipse (3.3 Europa) along with the Aptana plugin, RadRails plugin and Subclipse plugin.
The version of Eclipse that is in Hardy Heron’s repository is 3.2.2 or so, which is a few versions behind. So I decided, instead of installing from the repository, I should really install the latest available version.
Eclipse Ganymede comes in many different packages, and each package has downloads for different operating systems (namely Windows, Mac OS X, Linux 32bit, Linux 64bit). Since I am installing on my desktop (Ubuntu), I chose Eclipse Classic 3.4 (151 MB) (Linux 32bit).
After the download completed, I extracted it to /home/hantu/development/:
mv eclipse-SDK-3.4-linux-gtk.tar.gz ~/development
tar xvf eclipse-SDK-3.4-linux-gtk.tar.gz
Note: Initially, I wanted to install it under /opt/, but there is an issue regarding the file permission which I am not able to figure out. Something along the lines of .filetablelock Permission Denied. I’ll figure this out eventually, someday..
Surprisingly, Eclipse just works!
Next, without repeating what others have already documented, install Aptana Studio as a plugin of Eclipse. Don’t forget to install the Ruby on Rails support from Aptana’s Start Page (Help -> Aptana Studio Start Page…).
Subclipse is a plugin to work with SVN, instead of the default CVS support of Eclipse. Subclipse might not work with Eclipse 3.4, but I followed the instructions offered by a good man, so far, things are working perfectly.
More notes: I said ‘on Linux’ because I assume it is the same, for different distros, since downloading from Eclipse’s website does not require you to compile from source. Correct me if I am wrong.
Posted in Monash, Ruby on Rails, Technology, Ubuntu | No Comments »
Thursday, June 19th, 2008
While working on our industrial project today, we stumbled across this situation where we need a base class (Document) that are able to refer to different type of documents (ie. Report, Screen, etc).
We found two ways of dealing with this situation, one of which is by using Single Table Inheritance (STI); and the other one we are fond of, is called Polymorphic Association.
According to [1], Single Table Inheritance is:
… an inheritance hierarchy of classes as a single table that has columns for all the fields of the various classes
We decided to go with Polymorphic Association, because we wanted different classes for different subtypes of documents. We wanted to avoid having a really large single table to store everything, and using STI might leave us with many empty fields in a row.
A little explanation of Polymorphic Association, based on my understanding. Polymorphic Association uses two fields in the base class as the foreign keys, to refer to different subclasses. It stores the ID of the subclass, and the type (model).
This is the relationship we hope to achieve:
I have created models for the above classes:
script/generate model Document
script/generate model Report
script/generate model Screen
script/generate model Process
We start by declaring the interface, and associate them:
We declare the interface in models/document.rb:
class Document < ActiveRecord::Base
belongs_to :content, :polymorphic => true
end
Then, we associate the subclasses with the interface.
In models/process.rb:
class Process < ActiveRecord::Base
has_one :document, :as => :content
end
In models/report.rb:
class Report < ActiveRecord::Base
has_one :document, :as => :content
end
In models/screen.rb:
class Screen < ActiveRecord::Base
has_one :document, :as => :content
end
Now, in Document’s migration file, create the table:
create_table :documents do |t|
t.string :title
t.string :author
t.references :content, :polymorphic => true
t.timestamps
end
In the rest of the migration files (create_report, create_screen, create_process), fill in the appropriate columns for your table, and finally run the migration.
rake db:migrate
What you will see in your database table (documents) is that Rails created two fields, namely content_id and content_type in your table. Those are the fields used to reference the subclasses.
So far, this method is able to accomplish what we had in mind.
If anyone out there have a better solution to this problem, drop me a comment. Cheers.
ref:
- P of EAA: Single Table Inheritance
Posted in Ruby on Rails | No Comments »
Tuesday, June 17th, 2008
In Rails, there are two ways (that I know of) to define many-to-many relationship, one of which is by using has_and_belongs_to_many (a.k.a HABTM), and the other one is by using through association.
Differences between the two methods can be seen in this comparison. Episode 47 of Railscasts also give a good understanding of the two methods, which is how I learned about the two different methods to define many-to-many relationship.
Our examples below are based on the following scenario (a simplified version of the project I am working on):
An application can have many categories.
A category has many applications.
We need some sort of join table to define the categorisation.
I have generated two models: app and category.
script/generate model app name:string
script/generate model category name:string
HABTM Method
In order to generate the join table, I did:
script/generate migration create_apps_categories
In your migration file:
def self.up
create_table :apps_categories, :id => false do |t|
t.references :app
t.references :category
t.timestamps
end
end
In models/app.rb:
class App < ActiveRecord::Base
has_and_belongs_to_many :categories
end
In models/category.rb:
class Category < ActiveRecord::Base
has_and_belongs_to_many :apps
end
Through Association
A different approach is used here, particularly useful when you need extra fields in your join table.
Create a model called Categorisation:
script/generate model categorisation app:references category:references
Now, in models/categorisation.rb:
class Categorisation < ActiveRecord::Base
belongs_to :app
belongs_to :category
end
In models/app.rb:
class App < ActiveRecord::Base
has_many :categorisations
has_many :categories, :through => :categorisations
end
Finally, in models/category.rb:
class Category < ActiveRecord::Base
has_many :categorisations
has_many :apps, :through => :categorisations
end
All these information are based on my understanding, I don’t know if its the right way of doing it, since I am fairly new to Rails. Leave a comment if there’s a better way, or if I am doing it wrongly.
Posted in Ruby on Rails | 1 Comment »
Wednesday, May 14th, 2008
I have been working on hyourinmaru quite often, and I feel that I neglected zangetsu. I decided that I should properly setup the (web) development environment I needed.
The following guide has been tested on Ubuntu 8.04 Hardy Heron only, not sure if it will work elsewhere.
(hyourinmaru is my MacBook running Leopard, zangetsu is my desktop running Ubuntu 8.04)
Installing MySQL
At the time of writing, 5.0.51b is the latest stable release. I downloaded the source of it from mysql.com.
sudo groupadd mysql
sudo useradd -g mysql mysql
tar zxvf mysql-5.0.51b.tar.gz (Specify the path, if the file is not in your current working directory)
cd mysql-5.0.51b
./configure --prefix=/opt/mysql
make
sudo make install
sudo cp support-files/my-medium.cnf /etc/my.cnf
cd /opt/mysql
sudo chown -R mysql .
sudo chgrp -R mysql .
sudo bin/mysql_install_db --user=mysql
sudo chown -R root .
sudo chown -R mysql var (According to MySQL [1], the data directory must be owned by mysql)
sudo bin/mysqld_safe --user=mysql & (We are still in /opt/mysql)
sudo bin/mysqladmin version (Just to see MySQL started correctly)
sudo bin/mysqladmin variables (More checking)
sudo bin/mysqladmin -u root shutdown (Check if we can shutdown MySQL)
sudo bin/mysqld_safe --user=mysql --log &
bin/mysql -u root
DROP USER ''; (We are in the mysql shell, I removed anonymous user altogether)
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yourpass');
SET PASSWORD FOR 'root'@'zangetsu' = PASSWORD('yourpass');
sudo bin/mysqladmin -u root -p shutdown (Now you need the password to shutdown)
sudo bin/mysqld_safe --user=mysql --log &
Installing Apache HTTP Server
Apache HTTP Server 2.2.8 is the latest version, and can be downloaded from here.
tar zxvf httpd-2.2.8.tar.gz
cd httpd-2.2.8
./configure --prefix=/opt/apache --enable-so --enable-rewrite (You can always change the options, depending on what you want)
make
sudo make install
cd /opt/apache
sudo vim conf/httpd.conf (Edit config file appropiately, I won't get into details here)
sudo bin/apachectl -f conf/httpd.conf (Tries to start HTTP server)
Browse to http://localhost/ and if you see ‘It Works!’ (I did not change DocumentRoot), then the server is running fine.
Adding apachectl to /etc/init.d/:
sudo cp bin/apachectl /etc/init.d/apachectl
sudo chmod +x /etc/init.d/apachectl
Following the recommendation from David Winter [2], time to secure the install:
sudo useradd -g nogroup apache
sudo vim /opt/apache/conf/httpd.conf
Change:
User daemon
Group daemon
to:
User apache
Group nogroup
Restart Apache and check if it is running as user apache
sudo /etc/init.d/apachectl start
ps aux | grep httpd
Installing PHP 5
I obtained the latest PHP 5 release from the php.net.
tar zxvf php-5.2.6.tar.gz
cd php-5.2.6
./configure --prefix=/opt/php --with-apxs2=/opt/apache/bin/apxs --with-mysql=/opt/mysql --with-gd --enable-soap --with-zlib (If you want to include other things, do ./configure --help)
make
sudo make install
sudo vim /opt/apache/conf/httpd.con
I added the below two lines between <IfModule mime_module> and </IfModule>
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
Now start your HTTP server, and try it out. Works like a charm for me!
Installing Ruby
The latest available Ruby version is 1.8.6, at this time.
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6-p114.tar.gz
tar zxvf ruby-1.8.6-p114.tar.gz
cd ruby-1.8.6-p114
./configure --prefix=/opt/ruby
make
sudo make install
After all those are done, check your installation by:
/opt/ruby/bin/ruby --version
It should return something like:
ruby 1.8.6 (2008-03-03 patchlevel 114) [i686-linux]
Installing Rails
I am using RubyGems to install Rails, the latest RubyGems is 1.1.1.
wget http://rubyforge.org/frs/download.php/35283/rubygems-1.1.1.tgz
tar zxvf rubygems-1.1.1.tgz
cd rubygems-1.1.1
export GEM_HOME=/opt/ruby/gems
sudo /opt/ruby/bin/ruby setup.rb config –prefix=/opt/ruby
sudo /opt/ruby/bin/ruby setup.rb setup
sudo /opt/ruby/bin/ruby setup.rb install
Check your installation by:
/opt/rubygem/bin/gem --version
You should get:
1.1.1
Then check if there’s any updates available:
/opt/rubygem/bin/gem update --system
Now let’s move on to install Rails:
sudo /opt/rubygem/bin/gem install rails --include-dependencies
Checking Rails version:
sudo /opt/ruby/bin/rails --version
And I also installed the MySQL gem:
sudo gem install mysql -- --with-mysql-dir=/opt/mysql
Done!
Troubles!
After I created a test application using Rails, I tried to start the server using:
/opt/ruby/bin/ruby script/server
I came across this error: no such file to load — openssl
After some searching online, I found this [3] and [4].
sudo apt-get install libssl-dev
cd /path/to/sources/ruby-1.8.6-p114
cd ext/openssl
sudo /opt/ruby/bin/ruby extconf.rb
make
sudo make install
Finally, start the server again, browse to http://localhost:3000 - and it works!
ref:
- MySQL 5.0 Reference Manual
- Building Apache 2.2 from source for Ubuntu Dapper
- openssl error - ubuntu - Ruby Forum
- Using openssl - ruby-talk-google
Posted in Ruby on Rails, Ubuntu, Web | No Comments »
Monday, May 12th, 2008
Reading Ruby on Rails Power! The Comprehensive Guide by Aneesha Bakharia, figured out to have a local copy of RoR API documentation:
rails api_doc
cd api_doc
rake rails:freeze:gems
echo >vendor/rails/activesupport/README
rake doc:rails
Wait for a little while, then you’ll have the documentations generated, locally. It will be placed under api_doc/doc/api - I copied doc/api out to another directory so I can easily locate it using my web browser.
Files: 358
Classes: 496
Modules: 638
Methods: 2598
Elapsed: 74.072s
Posted in Ruby on Rails | No Comments »