Saturday 17 November 2018

OS X Unable to build mysql2 gem


The problem:
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
The solution:
gem install mysql2 -- --with-cflags=\"-I/usr/local/opt/openssl/include\" --with-ldflags=\"-L/usr/local/opt/openssl/lib\"

Note that these are mainly notes to myself, but if you find them helpful then you're welcome.

Monday 22 October 2018

rails-sass is deprecated

I've been seeing this for a while, and have finally got round to finding a solution.

In the project `Gemfile`, remove the reference to `sass-rails` and replace with `sassc`.

Sigh. It took more than two minutes to find the fix, so logging it here

Saturday 7 July 2018

NameError: uninitialized constant RailsERD

This has been an occasional annoyance when setting up new Rails projects with the rails-erd gem.

The documentation for the gem is to add

gem 'rails-erd', :require=>false

into the Gemfile, and then run rails g erd:install to add the hook to run when a rails db:migrate is carried out.

I found that this creates /lib/tasks/auto_generate_diagram.rake containing

if Rails.env.development?
  RailsERD.load_tasks
end


One solution is to remove the :require=>false from the Gemfile, but apparently this may have an impact on efficiency rails erd issue 282.

I tried a slightly different approach, and changed /lib/tasks/auto_generate_diagram.rake to require the gem.

if Rails.env.development?
  require 'rails-erd'
  RailsERD.load_tasks
end

Sunday 21 January 2018

Publishing Rails 5 apps to heroku

When attempting to migrate the database, I was getting errors with the following two messages

Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).

Gem::LoadError: can't activate pg (~> 0.18), already activated pg-1.0.0. Make sure all dependencies are added to Gemfile.


The second one turned out to be the main issue.

A solution was found in a rails issue 31669, briefly

Thank you for the issue. Rails still don't support pg 1.0.

My solution was to change the Gemfile reference to

group :production do
gem 'pg', '~> 0.18'
gem 'rails_12factor'
end

And the migration succeeded. Now to apply the issue to the other app I was looking at yesterday...

Rails 5 and optional has_many relationships

This is a continuation in the random notes from attempting to learn Ruby on Rails.

Todays' rambling is to do with having an optional relationship between two tables.
erd from project


I am building an application to help me track fuel usage, and was wanting to have a relationship set between Users and Vehicles, based on the diagram.

The spec tests were failing, because the User:Vehicle relationship was mandatory.

The issue appears to be that Rails 5 has made the belongs_to relationship required (as found in the answer to a StackOverflow question).

The solution seems relatively simple, add the optional link to the relationship:
 
belongs_to :user, optional: true
 
My tests are all passing again. For now.