Error installing json 1.8.3 with ruby 2.4

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You are trying to install the gems for your rails application in a new computer, or by using ruby 2.4 and you get the following error:

Solution

Remove your old Gemfile.lock file and run bundle again. A newer version of json (ie 1.8.6) should be installed.

Routing rspec with nested routes

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You would like to add a routing rspec test in your rails application that tests for a nested route.
So while you have something like the following in your config/routes.rb file:

you want to add the following test for your route:

but you are getting an error like the following:

Solution

You will need to change your rspec to the following using any number for the user_id:

Solution adapted from here

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: …

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You have a Ruby on Rails application/engine with some tests in Minitest and using postgresql as the db. In the original development environment all the tests that use fixtures and foreign key validations pass, but when you try to move to a different environment with new database some or all of you tests fail with a message that refers to the ForeignKeyViolation as in:

Solution

Since active record disables the foreign keys before loading the fixtures in the database, the user that you run the tests with needs to have superuser permissions in the test database. Otherwise the fixtures are not loaded. So add the superuser permissions to your user:

You may need to drop and create your test database.

Running a specific Cop in Rubocop

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You are working on an existing rails project with many rubocop warnings and you would like to fix certain types or warnings by getting only the specified files.

Solution

So for example when you use rubocop you first get the following for all the files in the project:

You can get a grouping of the errors by using the following:

If you would like to know only the files for a certain type of warning to be able to fix them you could try the following:

Using Array.wrap in Rails when the results could either be a string or an array

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You want to use some array method in a value returned, which can either be a single value or an array of values.

Solution

Rails ActiveSupport Array, provides a method called wrap, that can be used to:

Wraps its argument in an array unless it is already an array (or array-like)

Full explanation of the method here.

Thanks Miles

SSL received a record that exceeded the maximum permissible length.

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You want to install rbenv, but when you are trying to clone the code with:

you get the following error:

Solution

If you are behind a proxy server make sure that you add your proxy server details in your ~/.bash_profile and source the file afterwards.

So first add the details of your proxy like:

And finally source the file before running the installation again:

Creating a simple rails docker image for testing in cloud deployment

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Trying to deploy a rails application in a cloud provider ie dreamhost, that uses OpenStack these are the steps needed:

  1. Install docker in your development machine and your cloud provider by following the installation instructions from here
  2. Create an account in Docker Hub, that will be needed later on to push your docker image with the application
  3. Pull the official rails docker image to your development environment:
  4. Create a new simple rails application:
  5. Change to the application directory and add a Dockerfile in the root directory containing the following:
  6. Build your new image by using:
  7. Check that your image was build by using:
  8. Start the container with:
  9. Make sure that you can see the initial rails page by using your browser to go to http://127.0.0.1:3000
  10. Push your image to your Docker Hub account by first logging in to it from the command line: , and then when you get ‘Login Succeeded’, push your image to your account:
  11. TBC

Capturing output with UnitTest in Ruby

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You have some ruby code, a rake task for example, that outputs some results in the standard output, but you would like to test it in your unit tests.
MiniTest has capture_io and capture_subprocess_io, but there is nothing similar in UnitTest.

Solution

Looking at the code of the above MiniTest assertions you can create your own assertion to be used in your tests.

Create a new file under your test folder called MyAssertions and use the following code (taken from MiniTest), for your capture_output assertion:

Then include it in your test_helper.rb file with:

and use it in your tests like:

Could not find libv8-3.16.14.9 in any of the sources

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You are trying to upgrade one of your gems (ie uglifier) and at the same time you are using a new gemset (ie for rails 4.2.4), and you get the following error when you are trying to bundle:

Solution

Upgrade to rails 4.2.4 first and bundle and afterwards change the other gem and do bundle again.

Setting R_HOME for PL/R in PostgreSQL in Ubuntu

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You want to use the PL/R extension in PostgreSQL, and after the package installation you need to set up the R_HOME in PostgresQL.

Solution

Add the following line to the bottom of your PostgreSQL configuration file as sudo: