Archive for the ‘thoughts’ Category


Rails: how to show SQL logs in console

posted by Arkham on September 2nd, 2011

Simply type in rails console:

ActiveRecord::Base.logger =

Tags: , , ,

Ruby: disable documentation when installing gems

posted by Arkham on July 29th, 2011

If you want to install gems quickly without waiting for ri and rdoc documentation files, run this command:

echo "gem: --no-ri --no-rdoc" >> ~/.gemrc

Your new gems won’t include documentation at all. Hooray!

Tags: ,

Rails 3: how to undo rails generate

posted by Arkham on July 29th, 2011

Have you ever run a

rails generate controller UsersController

and seen your app folder filled with useless files?

create app/controllers/users_controller_controller.rb
invoke erb
create app/views/users_controller
invoke test_unit
create test/functional/users_controller_controller_test.rb
invoke helper
create app/helpers/users_controller_helper.rb
invoke test_unit
create test/unit/helpers/users_controller_helper_test.rb
invoke assets
create app/assets/javascripts/
invoke scss
create app/assets/stylesheets/users_controller.css.scss

Eww, that’s gross.
To undo that rails generate, simply run:

rails destroy controller UserController

and rails will destroy all those ugly files for you!

remove app/controllers/users_controller_controller.rb
invoke erb
remove app/views/users_controller
invoke test_unit
remove test/functional/users_controller_controller_test.rb
invoke helper
remove app/helpers/users_controller_helper.rb
invoke test_unit
remove test/unit/helpers/users_controller_helper_test.rb
invoke assets
remove app/assets/javascripts/
invoke scss
remove app/assets/stylesheets/users_controller.css.scss

Tags: , ,

Ruby, difference from instance_eval and class_eval

posted by Arkham on July 5th, 2011

String.instance_eval { def foo; p "bar"; end } = "bar"
String.class_eval { def foo; p "bar"; end }
"sausage".foo = "bar"

So, using instance_eval adds a class method.
On the other hand, using class_eval adds a instance method.


class_eval is a method of the Module class, meaning that the receiver will be a module or a class. The block you pass to class_eval is evaluated in the context of that class. Defining a method with the standard def keyword within a class defines an instance method, and that’s exactly what happens here.

instance_eval, on the other hand, is a method of the Object class, meaning that the receiver will be an object. The block you pass to instance_eval is evaluated in the context of that object. That means that String.instance_eval is evaluated in the context of the String object. Remember that a class name is simply a constant which points to an instance of the class Class. Because of this fact, defining a method in the context of Class instance referenced by String creates a class method for the String class.

Tags: ,

Rails 3: difference between has_many :through and has_and_belongs_to_many

posted by Arkham on May 24th, 2011

From Rails guides:

The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the relationship model as an independent entity. If you don’t need to do anything with the relationship model, it may be simpler to set up a has_and_belongs_to_many relationship (though you’ll need to remember to create the joining table in the database).

Tags: , , ,