FRESH → the Hive

Fresh from the Hive is Studio Melipone's weblog about visual delights, startup love and much more.

As opposed to whatever SEO stuff you should pay attention to this blog is run in a complete natural non sense. Enjoy!

Why don't you grab our RSS?

 

#Rails Test if a column exists

A really small & simple trick to avoid headaches…

…When you want to test if a model’s column already exists or not.


Use :

  1. if User.column_names.include?(“is_cool”)
  2.  
  3. end

Instead of :

  1. if User.is_cool?
  2.  
  3. end

Because if it’s a boolean column, it will return the same result if the column do not exist and if its value is false… Which is totally different ! In one case the user is not cool, in the other, you do not know !

This case happens often with migrations adding a columns later in a project, having after_save/after_create methods, etc… And when someone need to clone your project the first time, you’ll avoid having crashes on every migrations !