Active Record’s << or push or concat method

Active Record’s << or push or concat method – Introduction

You have used it over and over again but yet there are some gotchas you have to keep in mind when you are using ActiveRecord’s << method of a has_may association.

I was always wondering how ActiveRecord handles the situations where you add a new object in a collection (via of course the << method) and the owner is also new, does it saves the object before adding it to the collection or waits for the owner to get saved and then saves the unsaved objects to the DB? This is one of the questions that are gonna get answered in the following post.

Continue reading

Rails console and ActiveRecord SQL queries – Rails quick tips

Rails console and ActiveRecord logger – Rails 3

Well, there are many many times (in fact always :P) that you want active record sql queries to log into STDOUT and especially when you are working in the rails console. I didn’t know how to do this and after a while of Google searching I stumbled upon this command that you type every time you open the rails console:

Continue reading

Designing Helpers in Ruby on Rails

Designing Helpers in Ruby on Rails – Introduction

I assume that many of you have written (and still writing) helpers for your views in your awesome Rails projects. A helper, as you all know, is a module which contains methods that are vital for the simplification and the dryness of your views. And because helpers are meant to be used within your views they often contain html code (either plain or ruby code that produces html) which, as you may have noticed, is not very flexible.

Continue reading

html_safe and helpers in rails 3. Mystery solved.

html_safe and helpers in rails 3 – Rails 2 vs 3

In Rails 3 (contrary to Rails 2) Strings are automatically escaped using ERB::Util.h(string) and the reason is simple. In 95% of the cases you won’t have to print html code inside ruby strings like this:
< %="<h1>Hello World"%>
In Rails 2 for this 95% of the cases you had to call the “h” helper to escape the output which was really annoying and insecure (if you forgot one).

Although this was the case for views, constructing a helper that prints html content became tricky in Rails 3. If you want your html content to stay unescaped it has to be an ActiveSupport::SafeBuffer instance instead of a String instance. To achieve this there are two solutions: Continue reading

How to write case statement into views – Rails quick tips

Case statement in a view – Fail!

Well I can’t recall when (and why) but one time I was writing a view in a Rails project and needed to write a case statement (of course I could have done it with usual if / else /elsif statement, but the case statement seemed more natural). My first try (and I remember I was pretty confident that this was the way to go) had gone like this: Continue reading

proc and lambda in Ruby

proc and lambda in Ruby – Introduction

Today we are going to talk a little bit about the infamous procs and lambdas in Ruby and the differences between them. Well, you may already know that a proc and a lambda are objects of the same class:

p, l = proc{}, lambda{}
#=> Proc
#=> Proc

So is proc an alias for lambda ? Continue reading

Multiple Table Inheritance in Rails 3

Multiple Table Inheritance – Introduction

It is a fact that sometimes we want to achieve a database design similar to the design of classes and subclasses in OOP. This design is what we call Multiple Table Inheritance (MTI). The idea behind it is to have a main table which will hold all the basic attributes of the underlying models and separate tables for the underlying models each of them will hold specific attributes special for each model.

Multiple Table Inheritance – Rails 3 Implementation

Enough of the theory, let’s get our hands dirty and make it work in the Rails framework. Continue reading