Rails

PG::ObjectInUse: ERROR - Let's fix this

Three options I use to move past this bitch

Andy Huynh

1 minute read

Here’s why and how you fix this error: PG::ObjectInUse: ERROR: database "<MY_DATABASE>" is being accessed by other users DETAIL: There are 4 other sessions using the database. : DROP DATABASE IF EXISTS "<MY_DATABASE>" You can have a console open in a shell, your server running, another console open and maybe try opening another Rails app in a Tmux session. You’re spinning multiple Ruby processes and Postgres isn’t down with it. You can’t perform normal tasks like rake db:drop or migrate because you have active connections to your Postgres database.

Play Defense With Production Migrations

A Rails fallback solution for migrating data

Andy Huynh

2 minute read

Let’s say we have an Account class with an address column. We got word a home address AND a work address is in play. Apparently addresses in our app are complex creatures and we have a has_one/belongs_to relationship. We’ll have to rename and add a column. Renaming the column is the point of this post and it will get tricky, especially with production data. When migrating data, we have a short window where Heroku will take roughly 60 seconds to migrate the data.

Understanding RSpec be and eq Matchers

Avoid an error you'll see constantly with this trick

Andy Huynh

2 minute read

I confuse RSpec’s be and eq matchers when writing specs. It’s basically one or the other and the specs pass. Better you understand them to be a better programmer. Here’s my train of thought: scenario "User’s name is Jon" do let(:user) { Fabricate(:user, name: "Jon") } expect(user.name).to be "Jon" end Running this test we’ll get: Failures: 1) User’s name is Jon Failure/Error: expect(user.name).to be "Jon" expected #<String:70298288643660> => "Jon" got #<String:70298288643700> => "Jon" …

Understand Javascript Data Tags With Me

Data Attributes in Rails helpers

Andy Huynh

1 minute read

I was given the task of creating a copy to clipboard button for readonly inputs. This feature would be omnipresent in the codebase, a great candidate for a helper. The trickiness came when implementing attributes without values in Rails helpers. As an aside, copying to a clipboard is widely known to be trouble to work with across different browsers and mobile devices due to Javascript and security issues with a user’s clipboard.

Clean Your Schema

Dirty Rails Schema Fixes

Andy Huynh

1 minute read

You might get asked to switch tasks while working on a feature. Outstanding migrations are still in your schema and other conflicting migrations might put your schema in an inaccurate state. Here are some quick fixes: Rollback your schema In your branch with an outstanding migration, find the timestamp and run bundle exec rails db:migrate:down VERSION=<timestamp>. Checkout db/schema.rb and switch branches to continue working. Start with a fresh schema Akin to week old, stale curry in tupperware, sometimes it’s best to ditch the whole thing and start fresh.

Is it Ruby, or Rails Code?

Distinguish Between The Two With This Trick

Andy Huynh

1 minute read

A common mistake among new developers is asking premature questions. Top shelf developers are best utilized as a last resort. Rails developers are expected to read and understand a TON of code. Hopefully you’ll be reading good, idiomatic Ruby/Rails code. Jostling whether a class is Ruby or Rails is a common bump in the road. There are clues to distinguish the difference. For example, when I come across ActiveSupport::OrderedSOptions in an application, it’s blatant Rails code with ActiveSupport as a clear indicator.

Blueprint to Becoming a Better Rails Developer

The Intermediate RoR Developer Guide

Andy Huynh

2 minute read

Junior devs on the Rails Prime stack, listen up. I will assume you are starting to absorb less value in products like CodeSchool, TreeHouse and Codecademy. Struggling through personal projects and solving real world problems is the next logical step to self reliance as a developer. It is far from a small step considering juniors typically possess two years of professional experience, or less. Googling the proper questions to our problems could still be unnatural in any junior’s career.