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.