Closed scottohara closed 4 years ago
Unable to reproduce in dev using the repo example above?
Based on the 500 Internal Server Error
response, the only non-nullable column in the database called category_id
exists in the transaction_categories
table.
This suggests that the error originates from line 20 in the SubTransaction::create_from_json
class method:
My guess is that when the error occurs in production, the subcategory of the 3rd split ("Home Repair") was not actually nil (despite the typeahead being blank and showing the Subcategory
placeholder text).
Perhaps it's a zero-length string? The TransactionEditController.isString()
function in the front-end only returns true
when the value is both typeof string
and non-zero length. So a zero-length string wouldn't style the field as a warning (to indicate a new subcategory will be created).
On the server, assuming json['subcategory']
is an empty string, this expression at line 17 would attempt to create a new subcategory:
Category.find_or_new json['subcategory'], category unless json['subcategory'].nil?
The Category
model has validates :name, presence: true
so a zero-length string for name would generate an invalid category instance.
When SplitTransaction#save!
is eventually called, way down in the model SplitTransaction.transaction_splits.trx.transaction_category.category
would fail validation, causing the entire thing to fail.
Change the unless json['subcategory'].nil?
expression to if json['subcategory'].present?
; to account for the value being either nil or an empty string.
There are 2 other occurrences of the same code in BasicTransaction::create_from_json
and BasicTransaction#update_from_json
, so these should be updated as well.
Repro steps
Enter a new transaction as follows:
On save, console error shows:
...and the
POST /transactions
request returns a500 Internal Server Error
with the following: