Rails 5 ActiveRecord OR query chain

“Here is on how to handle Rails 5 ActiveRecord OR query chain.”

So it’s been a while I have been playing with Rails 5 and I have noticed several changes from Rails 4 and of course Rails 3.2.

I have been working with ActiveRecord and tried to construct an OR chained query. As I have been checking the Rails 5 documentation I ended up on a github pull request in which the Rails contributors were discussing the ability to chain two queries.

An example of would be:

Worker.where(name: 'Mark').or(Worker.where(supervisor: true))

where it will return the worker whose name is Mark or whoever is a supervisor.

Here are some other examples:

Finding the worker that is named Mark and is 55 years old or named Chris:

Worker.where(name: 'Mark').where(age: 55).or(Worker.where(name: 'Chris'))

Finding the worker that is named Chris or Mark and is supervisor:

Worker.where(name: 'Chris').or(Worker.where('Mark')).where(supervisor: true)

Questions? Feel free to ask away!

Handling Rails 4 Mysql2 Gem LoadError

“Upgraded to Rails 4 and getting the Mysql2 Gem LoadError?”

Last night I was asked to maintain a Rails 3 web project. My first suggestion was to upgrade from Rails 3.2 to 4.x. I went through a Rails updating guide and when I tried to launch the rails server I was getting this error:

Rails 4 - Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded

I ended up finding that the issue is not with mysql2 gem or with MySQL compatibility but with the ActiveRecord adapter.

To resolve this issue I just had to specify the mysql2 gem to a version like ‘~> 0.3.18’ or ‘~> 0.3.20’

Changing the line your Gemfile: gem ‘mysql2’ to gem ‘mysql2’, ‘~> 0.3.18’ will resolve the error.

Here is the exact code snippet for copying:

gem "mysql2", "~> 0.3.18"

You can read more about the issue that was reported on Rails Github repository here.

Rails installation failing Ubuntu

“Here is your solution to Rails installation failing Ubuntu.”

I have setup a Ruby on Rails developer several times on all the major operating systems (Linux, Windows and Mac OS X). I usually find it easier to build the environment on Mac OS X, more difficult on Windows but tiring and time consuming on Linux.

After a lot troubleshooting I came out with the conclusion that the issues are caused because of missing libraries. Most of the times what is missing are the development dependencies of ruby.

If you use RVM you can simply type the following command and get back the missing libraries:

rvm requirements

And then you can do sudo apt-get install missing_libraries

Alternatively you can install the following libraries/components:

sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libxml2-dev autoconf libc6-dev ncurses-dev automake libgmp-dev libtool

Most probably this will resolve your issue. If it does this means your rails installation fail was surely caused by the Ruby C dependency.

If not feel free to comment and we will resolve this together.

Heroku Database Transfer

“Heroku Database Transfer from one app to another.”

Backups of your data are crucial to any application, not just for recovering in case of a catastrophe, but also for testing, setting up staging environments, and migrating your data.

Gladly if you are using Heroku Web Services it is very easy to transfer one database from one Heroku app to another. Here is an example command:

heroku pg:copy source_app_name::source_database_name receiver_database_name --app receiver_database_name

You can execute the above command on your terminal.

Now if you wish to create a Database backup you can do:

 heroku pg:backups capture --app app_name

And then to restore it use the following command:

heroku pg:backups restore b001 DATABASE_URL --app app_name

You can find more on Heroku Database backup documentation.

Nokogiri LoadError Windows 10

“Lets fix Nokogiri LoadError Windows 10.”

Nokogiri on Windows 10 is not a great combination.

I do not tend to code with Ruby on Windows but I was curious after setting up my gaming Windows PC on how Ruby on Rails will work on this new operating system.

I ended up facing lots of issues but what stand out was the Nokogiri’s gem installation.

After a lot of research and asking around I found out that the issue was related to Ruby 2.2.

After a lot of different tries and using different versions I ended up having it working with using a different version.

Add this to your Gemfile and then bundle install:

gem "nokogiri", ">= 1.6.7.rc"

I suggest you gem uninstall nokogiri if you stumbled upon this issue while your app or environment was working before. My case was with a fresh new working environment.

Heroku Paperclip AWS S3 NameError

“Heroku Paperclip AWS S3 NameError: The end of your error’s journey. Here is the solution!”

Are you also stumbled upon this error? Don’t worry it is not just you.

Just quickly your solution to this error is to add this to your Gemfile:

gem 'aws-sdk', '< 2.0'

Curious on what causes this Heroku Paperclip AWS S3 NameError?

This error is caused by the newer version of the gem which is not backwards compatible with the previous version. Thus, we have to specify the major version (2.0) in our Gemfile. You can read more about it here.

Rails 4 Ticket Support System

“The result is an easy to use Rails 4 Ticket Support system.”

As we were soon going live with AwesomeWeb’s freelance marketplace, we were in need of a ticket support system.

At that time, I had to come up with something quick, convenient and friendly to use. I looked around of what other websites use and all I could see were 3rd party services with complex ticket support systems. I was not satisfied from such solutions and wanted to build something that will be on-site. Then I remembered about Digital Ocean’s ticket system. I used it couple of times in the past and it was really easy to use and straight to the point. After reviewing it a bit I decided to give it a try.

The result is a convenient, easy to use Rails 4 Ticket Support system that can be found in my github account. Feel free to fork/clone it and use it. Feel free to report any bugs/issues and improve it as much as you desire.

Make sure you read the README.md for the instructions, features and some special notices!

Devise error: email can’t be blank

Devise error: email can’t be blank? – Well it isn’t!

Many of you might have been frustrated with the Devise error: email can’t be blank.

You do indeed type an email in the text field but still get this error! I even took out the :validatable module from devise in order to get rid of this error. However that is not the solution.

The Rails 4 uses strong-parameters is causing this issue and the solution is simple

The solution is simple. You just have to place this code segment into your Application Controller (application_controller.rb):

  before_filter :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
end

Keep in mind to configure for each action like :sign_in, :sign_up, :account_update etc by doing something like: (if it is of course needed for the other actions as well)

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation) }
  devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email, :password, :password_confirmation) }
end

Rails 4 Static Pages – How To

Creating Rails 4 Static Pages is really straightforward.

We all at some point needed to create a static page like an About or Contact us page. Rails 4 Static Pages are easy to create and the procedure is slightly different than Rails 3.

In Rails 3 you had to use the match method like:

match ':action' => 'static#:action'

However in Rails 4 the Static Pages can be created by using:

get ':action' => 'static#:action'

By doing that you have setup your Static’s page route. However you also need to create your static_controller.rb and input your “action” there. After that you need to create the app/views/static folder and make an action.html.erb file.

Your Rails 4 Static Page is now ready and visible when you browse to http://localhost:3000/action

Accidentally typed password into terminal?

Have you accidentally typed password into terminal? Don’t worry!

The probabilities are that this happened to you already. When you type your password then it gets recorded into bash’s command line history. This is a serious security vulnerability and you should immediately take action!

These are some of the quick fixes I suggest:

  • Change your password. If you do not have this privilege or you do not prefer to change your password you can…
  • Delete your typed password from command line’s history. To do so, input history into the command line. The commands history will show up. Take a note of the line you are interested to delete and input “history -d [line_number]”. Then type “history -w” to save the changes you made to the history.