Add Index on Rails Active Record

Indexes are copies of columns of data from a database table. It makes is easily searchable and thus making it fast and efficient. In Rails an index can easily be adding via migrations with:

class AddIndexToTable < ActiveRecord::Migration
  def change
    add_index :tables, :column_id
  end
end

Unique

We can add a unique option to make the index unique:

class AddIndexToTable < ActiveRecord::Migration
  def change
    add_index :tables, :column_id, unique: true
  end
end

This, however, would not allow duplicate value for the same column.

Unique with Combination

We sometimes have join tables wherein we need to scope out combinations of ids. With this, we can add an index that references both columns:

class AddIndexToJoinTable < ActiveRecord::Migration
  def change
    add_index :join_tabes, [:column_id, :column_2_id], unique: true
  end
end

This will make sure that combinations of column_id AND column_2_id are all the time.

 CSS: Using Inline-block

There has been a lot of confusion when using the inline-block value of the display css property. This is a simple tutorial for you when you find yourself needing inline-block.

float is a way to cleanly use up the whole width of the browser even with block elements like div and p. There is a problem when using floats. The container of this floats do not wrap around the floating content. This has resulted in using clear-ing of elements.

.clearfix {
  clear: left;  
}

Floating with and without clearing

See the Pen Float test with and without clearing by Thorpe Obazee (@pinoytech) on CodePen.

Using inline-block

inline-block is a way to float elements but not needing to be cleared. Below is a clear way to use inline-block

See the Pen Float and inline-block example by Thorpe Obazee (@pinoytech) on CodePen.

 How to Delete a Git Branch Locally and Remotely

Git is a version control system that is very widely used in the software development world. A few companies offers this as a SAAS like Github, Bitbucket and Gitlab

How to delete a local branch

git branch -d the_local_branch_name

How to delete a remote branch

You will just need to push to the remote the branch name that was deleted locally on your machine.

git push origin :the_remote_branch

Git Related Posts

 How to Add Custom Labels to Active Admin Filters

Active Admin has been one of the most used Administration panel in the Ruby on Rails world. It’s so easy to use. Although, I must admit looking for the right resource for customization is pain.

Here is how to Add Custom Labels to Active Admin Filters

  filter :user_name, as: :string
  filter :user_email, as: :string, label: 'User Email'
  filter :logged_in_time, label: 'Logged In Date'
  filter :logged_out_time, label: 'Logged Out Date'

The filters would automatically use the column name appended by the table name if you are using an associated table’s field. The label field would allow you to add your own custom labels to filter.