Rails 5 – how to create a child before a parent

I’ve been hacking away on a personal budgeting app on my train rides and ran into a squirrely problem in rails 5.   My budgeting app uses the spectacular Quovo API (https://api.quovo.com/docs/v3/) to pull transactions from my Chase accounts.  Quovo rocks, it’s like Plaid or Yodlee, but has a free plan that gives you 100 free connections.

Anyway, the Quovo API brings back connections  and accounts, among other things.   The problem was that the webhooks came with the account information before the connection info.  I have my models set up with the connection as a parent to the child accounts.    Every time I tried to save the account without the connection created first, I got this error:

ActiveRecord::RecordInvalid: Validation failed: Connection must exist

30 minutes of diligent Googling led me to this great YouTube video by Ruby Thursday (https://twitter.com/rubythurs).

The answer?  The optional: true parameter, set like this:

class Account < ApplicationRecord
  belongs_to :connection, optional: true 

I added that and bang!  No more issues.


This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s