Must-have gem for rails projects: zero downtime migrations

This is one of the most useful Ruby gems I’ve discovered in a while: zero_downtime_migrations, it catches problematic migrations at development time.

Problems such as:

  • Adding a column with a default in a single migration;
  • Adding a non-concurrent index;
  • Mixing data changes with index or schema migrations;
  • Performing data or schema migrations with the DDL transaction disabled;
  • Using each instead of find_each to loop thru ActiveRecord objects when doing data changes;

And it doesn’t only catch the problem but it also teaches what you should do to avoid it 👏👏

Example from their README:

Solution of problem and solution

Seriously, share this gem with every Rails developer you know and make everybody’s life a bit easier 😁

Beholddy, easily monitor youtube for new videos of specific subjects

On this last weekend, me and a couple friends hacked together a beautiful UI for a tool that I built to scratch my own itch: monitor youtube for new videos on specific subjects. Meet Beholddy:

This cute beholder, with its unique features, will monitor youtube for every new video uploaded and let you know as soon as a new that meets your criterias comes out!

I currently use it to find out:

  • videos of huge channels of a topic that I could take advantage on (by getting into their “similar videos”, for example);
  • discover smaller channels talking about subjects similar to mine and maybe propose a partnership;
  • monitor competitors;
  • new tips about videogames I enjoy 😜

But hey, this is me! Try it and maybe you will find other uses for it and don’t worry cause it has a free plan!

And please, give me some feedback on how you are using it and I will even add a couple more alerts to your free plan 😉

Someone coding

How to delete local git branches after the merge

From time to time we all get annoyed by the git branch autocomplete, which used to be so useful, but now makes us practically write the whole branch name! 😡

Why it keeps happening? Because we are awesome developers who use feature branch flow and now, after working so hard, we have dozens of branches on our local machine.

Yogo, can you tell me an easy way to clear these branches? I don’t wanna delete each one manually! 😩

I sure can, my friend! Here is an alias that I use to delete all my merged branches:

alias clear_merged_branches="git branch --merged | egrep -v '(^\*|master|production)' | xargs git branch -d"

It will only leave the “master” and “production” branches, which you can easily change on that code. Besides the branches that haven’t been merged into master, of course.

Remember to use this alias while you are on the “master” branch 😉

Update

As lpmusix pointed out, you can also use it as a git alias. Add to the alias section of your ~/.gitconfig:

cb = "!git branch --merged | egrep -v '(^\\*|master|production)' | xargs git branch -d"

Ruby already has its own regular expression to validate emails

I’ve searched and written a lot of regular expressions to validate email on our models and forms but recently I’ve found out that Ruby already has a good one and its easy to access and use.

All you have to do require uri library and use its constant:

require 'uri'
'user@gmail.com'.match(URI::MailTo::EMAIL_REGEXP).present?

It’s also easy to use on an Active Record format validation:

validates :email, format: { with: URI::MailTo::EMAIL_REGEXP, message: "only allows valid emails" }

It even accepts emails with “+” as gmail enables.

 

How to solve rails 4 UndefinedTable Error when creating namespaced models

In a given rails 4 application, I have two namespaced models with a has_many association between them:

# models/review/asset_type.rb
module Review
  class AssetType < ActiveRecord::Base
    belongs_to :review_asset_category, class_name: Review::AssetCategory
  end
end

# models/review/asset_category.rb
module Review
  class AssetCategory < ActiveRecord::Base
    has_many :review_asset_types, class_name: Review::AssetType,
             foreign_key: 'review_asset_category_id'
  end
end

With migrations as:

class CreateReviewAssetCategories < ActiveRecord::Migration
  def change
    create_table :review_asset_categories do |t|
      t.string :name
      t.timestamps null: false
    end
  end
end

The migrations ran well but everytime I ran the tests I received the error:

Failure/Error: it { is_expected.to respond_to(:name) }
     ActiveRecord::StatementInvalid:
       PG::UndefinedTable: ERROR:  relation "asset_categories" does not exist
       LINE 5:                WHERE a.attrelid = '"asset_categories"'::regc...
                                                 ^
       :               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                            pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                       FROM pg_attribute a LEFT JOIN pg_attrdef d
                         ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                      WHERE a.attrelid = '"asset_categories"'::regclass
                        AND a.attnum > 0 AND NOT a.attisdropped
                      ORDER BY a.attnum

The solution is simple, just add this class method to each model or to the module that is namespacing the models:

module Review
  def self.table_name_prefix
    'review_'
  end
end

I hope this simple advice will save some time for other developers 🙂

MegaMaker Challenge

Justin Jackson started the Mega Maker Challenge a week ago and I joined right away:

The new year is here. How can you make this year better than the last? For me the answer is to create new things. I don’t want to “just be a consumer.” Making things, and releasing them to the world, gives my life purpose.

This year, I’m challenging myself to make 100 things. Want to join me?

Mega Maker Challenge logo

I sure want! And you can as well. As he says, you don’t need to create 100 things, you can challenge yourself to create as many as you want.

He even created a newsletter with good insights to help you conquer this challenge. Join the challenge and let’s make all the things!

Exercism – Practice your dev skills

A week ago I  was trying to find some algorithm exercises and I found exercism.io, a website that helps you practice your developer skills using your terminal, your favorite IDE and TDD 🙂

Captura de Tela 2015-12-28 às 18.12.33

Besides the TDD approach, what I really liked about it is that it encourages you to solve problems iteratively, one test at a time and when all tests are passing, refactor your code for a better solution.

It also lets you discuss each others solutions and I’ve already received some nice advices. Some exercises also made me learn some new methods that I’ve never used before like reducetr and learn that count can receive a block!

I’m practicing Ruby but they have exercises for a lot of other languages like: PHP, Lua, Lisp, F#, Javascript and Elixir.

I suggest you try it and then let me know if you liked it or not and if you did, comment here your profile so we can try to help you improve your coding skills 🙂

ps: you can also see my answers and help me improve mine