Pirun Seng

Ruby on Rails Developer

Three Main Headers Required for Rails API Authorization

Rails API Authorization

Once building API end points, it is good practice to authorize incoming requests just like we do with our application.

As a Rails developer, I’ve also involved in building API end points along with test coverage, too. We use devise for application authorization, and devise_token_auth for API authorization.

So far, we use Postman to test our API, you may use the same or a different one. However, the three main Headers which is required to request authorized resources are: access-token, uid, and client.

In order to get these Headers, we do need to send a post request to sign in a user first. If you signed in successfully, we will get a User object in the Body tab, and we can find our three main headers in Headers tab.

Happy coding,

Setup Docker Machine With VM and Known Issues

Hi There, today I am going to share steps, but not actually how as I will leave a reference to the Docker docs at the end, to set up Docker Machine and some known issues I have experienced.

  1. Install Docker
  2. Install Docker Machine
  3. Download and Install VirtualBox (VirtualBox-5.2.18-124319-OSX.dmg)
  4. Create Docker Machine
  5. Known issues:
    • Failed to install VirtualBox on MacOS. You may find this link helpful; How to Install VirtualBox on macOS Mojave and High Sierra
    • Failed to create docker machine. The solution that works for me is just to reboot machine. After reboot, I could create docker machine successfully.
    • docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? => This means that your Docker is not running. So you may just run your Docker app, and see that it is running. That should fix this problem.

Reference: https://docs.docker.com/machine/install-machine

Nice day,

Common Mistakes We Might Make in Rails Development

1 - Irreversible Migration

Rails 3 has introduced change method which help reduces lines of migration code from two method up and down to just one which is change method. For example, we want to change default value of nil to an empty string ''.

def change
  change_column_default :articles, :owner, ''

There is no problem when we run rake db:migrate. Yet, we don’t realize that we have made a mistake until we rollback the migration at some point. Then there will be an exception of ActiveRecord::IrreversibleMigration.

To get rid of the ActiveRecord::IrreversibleMigration exception, we have to rewrite both up and down methods again.

def up
  change_column_default :articles, :owner, ''

def down
  change_column_default :articles, :owner, nil

Heh! If you’re using Rails 5, there is a better way that help improve this case by adding from (old default value or nil) and to (new default value) options.

def change
  change_column_default :articles, :owner, from: nil, to: ''

By specifying the from and to options, we can freely run our migrations up and down without getting any of ActiveRecord::IrreversibleMigration exceptions anymore.

2 - Accidentally Deleted Object(s)

To be continued…

Modify EBS Volume or Disk Space of Amazon EC2

Amazon has provided us cool and easy ways to modify EBS volume, or in other word is to modify disk space of our instance or server.

I am going to sum up of how we can achieve it by using AWS Console. However, the FIRST thing you need to know is that you CAN increase the volume, but CANNOT decrease.

Go on if you are about to increase your volume!

If we are running multiple instances and volumes, the first thing we need to know is which volume that is associated with which instance we want to modify.

If we open the Amazon EC2 console and navigate to Volumes page, we will see a list of some volumes if we are running multiple instances. For somebody who has already named their volumes, that is okay for them, but as my first experience, I haven’t named any of the volumes, so I don’t know which Volume of my instance I am looking for to modify.

Worry not! To know which instance our volume is attached to, just click on the volume, and in the Description tab below, we can see the instance in Attachment information. After find out the instance it is attached to, I recommend to name your volume at this point, if you haven’t, so it is helpful when we come back later and that we can catch it up immediately.

Okay so now we know which volume to be modified! BUT! Wait! It is recommended to create a Snapshot of your current volume before modifying.

I will just leave a link for How to create a Snapshot. It might take some time due to how big your volume is.

Once you already create a snapshot, then you’re ready for modifying the volume.

Let select a volume, and choose the dropdown Actions button on the top, and select Modify Volume. We will see a modal window of Modify Volume, verify the Volume Type, adjust the size in GiB you like, and click Modify button, and Yes.

Last, modifying volume size has no effect until we tell the file system to get advantage of new storage size. This is how you can Extending a Linux File System after Resizing the Volume. In my case, I just need to run the following two commands.

sudo growpart /dev/xvda 1
sudo resize2fs /dev/xvda1

Please… keep in mind that when we want to modify something like this, eg. Volume (disk space), Data, etc, we do have to do a back-up, like we were creating a Snapshot of the volume, so it safe in case we want to rollback or something. Plus, it is even more better if you can try it on a test instance and volume first before working on the live one.

Reference: Modifying an EBS Volume from the Console


Continuous Integration With Codeship

Codeship is one of a very cool Continuous Integration (CI) platform. It comes with pre-installed various programming languages, frameworks and dependencies.

With a simple, yet powerful web UI and UX design, it makes us easy to get started in just a few minutes.

Codeship allows developers to connect to different repositories such as Github, Gitlab and Bitbucket. On top of that, we can setup our testing environment and deployment targets. Whenever the code is pushed to the repository, then Codeship gets triggered and run our test. There is an API of the build status in which we can put it in somewhere like the README of our project. We can either enable or disable the build notification on our own.

Codeship Basic is free!! Register for a free account, then we are ready to ship our code so that we can sleep well at night.

Getting started

Assume that we are already signed in. On the top navigation bar, click on Projects, you will as the screenshot above, layout might be changed due to current date.

By clicking on New Project, you will be asked to choose which repository you want to connect to.

In my case, I click on Github and I’ll come to this page in which I will have to put corresponding Git clone URL so that my repository and Codeship is connected.

At the project types selection section, click on Select Basic project

Last but not least, we need to choose the setup command. These are the pre-installed languages, frameworks and dependencies as I have mentioned above. Choose the one you are working on. In my case, I will want to choose Ruby on Rails since I’m building a web app with Ruby on Rails.

Click Save and go to dashboard, you should see your first build status.

The very last step (optional but recommended), we should display the build status on our repository using Codeship build status API. To do so, you might want to click on the build status, then click on Project Settings.

On the General tab, and at the Status Images section, we can use any of the image we would like to show the current status of our project. In my case, I use the Markdown Syntax. I put it in the README of my project, and here how it looks on my Github repository of the project I have connected to Codeship.

It has been a long day already, hasn’t it?

Hope you enjoy it!

Active Record Finder Methods - Find or Find_by

There might be sometimes that we get an unexpected exception where we use ActiveRecord::FinderMethods find and find_by.

So we need to be sure whether to use find or find_by where we are finding active record.

The main difference between find and find_by is the answer to this question, what we want to return once the record is not found? We will want to use find_by when we want to return nil if no record is found while we use find to expect the exception, ActiveRecord::RecordNotFound, to be raised.

To discover more about Active Record Finder Methods, please refer to the API document.

Happy coding!

How to Upload Files From Remote Location With Carrierwave

Carrierwave is a classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks.

I am going to demonstrate How to: Upload files from a remote location to JSONB or Array image column. For installation and configuration, I would recommend you to read Carrierwave document.

Let say we have an Avatar model with an image column of array data type. To create a new Avatar record with an image:

avatar = Avatar.new
avatar.remote_image_urls = ['https://image.flaticon.com/icons/png/512/25/25231.png']

Note: We use remote_image_urls for jsonb or array data type column while we use remote_image_url for string data type column.

Please feel tree to replace image to any column name you use to store the file or image. e.g If your Avatar model has a column named photo, then you might want to modify the syntax above to remote_photo_url and remote_photo_urls.

You can dig deeper to its source code here.

Happy coding!

Fast Finder

As a searcher, I usually spend much time looking for what I want and the right thing. Anyway, I sometimes feel not really good to spend much time looking for the things, but I still cannot find the right resources or solutions.

This makes me create a simple app called Fast Finder which records all right resources and solutions I have found. The main objective is to find the right thing faster. I hosted it on Heroku.

Recently, most resources are for Ruby and Ruby on Rails as I am a Rails Developer. Anyway, it will be for many more languages, programming languages, frameworks and technologies as it is growing and from your contribution.

Check out the link here: Fast Finder

Nice day!

Make Use of Library We Use

When I was a fresh Software Developer, I usually looked for libraries which were suit for programming language I used Ruby, in my case.

It is awesome that there is a Ruby on Rails which is a framework for Ruby. Plus, there are lots of Ruby Gems that are designed to help building software with Ruby and Ruby on Rails as well.

Jump to the point, when I used any Ruby gems, I just built thing with its main and basic usage. Once I came up with an idea to build something more advanced along with that library, my brainstorm was to write the things from scratch. Then, I did research on google for some concepts.

Soon, there was another idea came to my brain. “Why didn’t I read more document about the library I were using to see whether or not it had what I wanted?”

Bang! It was just a few minutes searching the feature in its the document, I found the exactly thing as I wanted.

To sum up, it is good to know what main features exist in the library we use, but it is much better to discover everything it can do.

Happy coding!

Undo Git Merge


Git is awesome and that I’ve used it along with my career, software development, so far.

I sometimes get confused doing git merge between branches. When it is a tough time preparing to release my feature to test server in which I have to merge the latest master branch, into my current working branch, let say update-layout branch.

Unexpectedly, I have merged update-layout into master. Actually, I want to merge master into update-layout.

Luckily, I haven’t pushed the master yet so that I can revert/undo merge safely.

To undo the merge, it is as simply as the following:

  • Stay on master branch by git checkout master
  • Type git log on the terminal to see the commit histories.
  • Find the latest commit of master branch we want to revert to.
  • Copy the commit sha, e.g. dbf0e49.
  • Back to terminal and type git reset --hard dbf0e49
  • It’s done! We’re on the latest commit as we want to revert/undo merge.

If we’re ready to merge update-layout into master or redo merge, it can be done easily for just do the merge again.

Happy with Git!