Thursday, 15 August 2013

Learning Ruby on Rails

 1. Polymorphic Association
http://railscasts.com/episodes/154-polymorphic-association
http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

For instance, a book can be liked and a video can also be liked. To make a generalised association which can like both a book an a video, we can simply make a polymorphic association named likeable which contains the likable_type and likable_id. 

2.  Choose between has_many :through and has_and_belongs_to_many
http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many

For instance, a book has many tags and a tag can be associated with many books. To make a simple many-to-many relationship, we can simply use has_and_belongs_to_many. However, to create an indirect association through a joint model, we can use has_many:through.

3. execJs: 'Could not find a JavaScript runtime' error.

To add gem 'execjs' gem 'therubyracer'in the gem file.

4. gem mysql sometimes triggers the following error:"incompatible character encodings: UTF-8 and ASCII-8BIT". Instead of using mysql, we need to use mysql2 which solved the problem. To install mysql2, you'll probably need to install the following package
sudo apt-get install mysql-client libmysqlclient-dev

5. To run rails app on port 80, the following command should be used
rvmsudo rails server -p 80 or rvmsudo rails server -p 80 -d

To stop the process, check the process id by typing cat tmp/pids/server.pid and then kill -9 the returned value.

6. Some very useful toolboxes.
Devise https://github.com/plataformatec/devise
Omniauth https://github.com/intridea/omniauth
Paperclip(rely on imagemagick) https://github.com/thoughtbot/paperclip,etc.
Kramdown http://kramdown.rubyforge.org/