Openfoodnetwork: Fix error `Money::Currency::UnknownCurrency: Unknown currency ''` on setup

Created on 12 May 2021  ·  3Comments  ·  Source: openfoodfoundation/openfoodnetwork

What we should change and why (this is tech debt)

This error is interesting: it pops up once when running script/setup, but after either:

  • a fresh re-install of the repo, or
  • replacing Money.default_currency = Money::Currency.new(Spree::Config[:currency]) with Money.default_currency = Money::Currency.new('AUD') and then running the script/setup and then reverting the change the error disappears 🤔

Context

https://openfoodnetwork.slack.com/archives/C2GQ45KNU/p1620397593134200
Linked issue: #7613

Impact and timeline

Timeline: 1 week. Impact: better onboarding experience for new developers ✨

tech debt

Most helpful comment

Could it be related to cache?

What if you try doing rails tmp:cache:clear in between?

I noticed another weird thing, when I start rails console 2 times in a row after clearing the cache, it can load 2 different values as the default currency.

  • First time it uses the value from Spree config "USD"
  • Second time (and always from now on) it uses the value from config/application.yml "AUD"

Might it have something to do with Figaro loading the .yml file after Spree is loaded in some cases? :thinking:

Related parts:
https://github.com/openfoodfoundation/openfoodnetwork/blob/99cf23df265ea97a1b6485c08d464da3298670b9/app/models/spree/app_configuration.rb#L41
https://github.com/openfoodfoundation/openfoodnetwork/blob/99cf23df265ea97a1b6485c08d464da3298670b9/config/initializers/money.rb#L2

All 3 comments

Could it be related to cache?

What if you try doing rails tmp:cache:clear in between?

I noticed another weird thing, when I start rails console 2 times in a row after clearing the cache, it can load 2 different values as the default currency.

  • First time it uses the value from Spree config "USD"
  • Second time (and always from now on) it uses the value from config/application.yml "AUD"

Might it have something to do with Figaro loading the .yml file after Spree is loaded in some cases? :thinking:

Related parts:
https://github.com/openfoodfoundation/openfoodnetwork/blob/99cf23df265ea97a1b6485c08d464da3298670b9/app/models/spree/app_configuration.rb#L41
https://github.com/openfoodfoundation/openfoodnetwork/blob/99cf23df265ea97a1b6485c08d464da3298670b9/config/initializers/money.rb#L2

Ah the mystery deepens! Thanks for the input @viktorsmari ! I'll try clearing my rails cache and see if the error re-appears 👍

I didn't know about the second point (default currency switching from "USD" to "AUD"). It points perhaps to a bigger problem: that we have two configs rather than one. For this ticket I guess we should fix only the default currency error and either always use "USD" or "AUD". But I'm aslo wondering if we should raise a separate ticket to merge all configs into one..? 🤔

So with @apricot12 we made a videoconference to talk about reproducibility of this issue. Here is a way to currently reproduce it systematically:

cd /tmp/
git clone $SOME_OFN_REPOSITORY_CLONE
cd /tmp/openfoodnetwork
bundle exec rails server
./script/setup

So, much thanks to @apricot12 who excavated the reproduction process. :pray:

Was this page helpful?
0 / 5 - 0 ratings