envato / double_entry

A double-entry accounting system for Ruby applications.

Home Page:https://rubygems.org/gems/double_entry

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DoubleEntry::UnknownAccount: account: spending scoped?: true

KelseyDH opened this issue · comments

On a Rails 5.1.4 app installing double_entry using Postgresql, with the following initializer:

require 'double_entry'

DoubleEntry.configure do |config|
  config.define_accounts do |accounts|
    user_scope = accounts.active_record_scope_identifier(User)
    accounts.define(:identifier => :savings,  :scope_identifier => user_scope, :positive_only => true)
    accounts.define(:identifier => :checking, :scope_identifier => user_scope)
  end

  config.define_transfers do |transfers|
    transfers.define(:from => :checking, :to => :savings,  :code => :deposit)
    transfers.define(:from => :savings,  :to => :checking, :code => :withdraw)
  end
end 

and unchanged migrations (beyond specifying ActiveRecord version 5.1).

I get the following error in console:

=> account = DoubleEntry.account(:spending, :scope => User.first)
  User Load (0.3ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1  [["LIMIT", 1]]
DoubleEntry::UnknownAccount: account: spending scoped?: true

Is this possibly related to #115? I've tried every combination I can think of for passing in a scope here and it's all failing.

My app already has an Account and Transfer model defined, but surely this shouldn't be of issue given double_entry's module namespacing? Any help much appreciated.

Hi @KelseyDH,

It looks like you haven't defined a 'spending' account in your configuration block.

Try something like this:

accounts.define(:identifier => :spending,  :scope_identifier => user_scope)

@orien Thanks for noticing that mistake. That came about from trying many combinations here without success, including with properly declared account names in the configuration, e.g. like with this setup:

With a configuration block correctly loading a savings entry:

require 'double_entry'

DoubleEntry.configure do |config|
  config.define_accounts do |accounts|
    user_scope = accounts.active_record_scope_identifier(User)
    accounts.define(:identifier => :savings,  :scope_identifier => user_scope)
    accounts.define(:identifier => :checking, :scope_identifier => user_scope)
  end

  config.define_transfers do |transfers|
    transfers.define(:from => :checking, :to => :savings,  :code => :deposit)
    transfers.define(:from => :savings,  :to => :checking, :code => :withdraw)
  end
end

calling it still fails:

 account = DoubleEntry.account(:savings, :scope => User.first)
  User Load (0.7ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1  [["LIMIT", 1]]
DoubleEntry::UnknownAccount: account: savings scoped?: true
from /Users/username/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/double_entry-1.0.1/lib/double_entry/account.rb:44:in `find'

@eadz Nope, just leads to the same error.

=>  account = DoubleEntry.account(:savings, :scope => User.first.id.to_s)

  User Load (0.5ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1  [["LIMIT", 1]]
DoubleEntry::UnknownAccount: account: savings scoped?: true
from /Users/username/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/double_entry-1.0.1/lib/double_entry/account.rb:44:in `find'

I got it working on a fresh install of Rails 5.1.5 with devise. So I suspect there must be something in my project that's overriding the user scope.

Of note, I already have ActiveRecord Account and Transfer models, and on the user side, I'm using devise_token_auth and papertrail which also do their own things to activerecord internals.

No idea where to start on debugging this, however.

Interesting to note: after I added a plain Transfer and Account model to my sample rails 5.1 double_entry installation, I got errors on this same command, but in a different way.

E.g.:

irb(main):012:0> account = DoubleEntry.account(:savings, :scope => User.last)
  User Load (0.6ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1  [["LIMIT", 1]]
DoubleEntry::AccountScopeMismatchError: Expected instance of `User`, received instance of `User`
	from (irb):12
irb(main):013:0> account = DoubleEntry.account(:savings, :scope => User.last.id.to_s)
  User Load (0.6ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #{Account account: savings scope: 2 currency: USD}

Whereby after I followed your debugging tips with the string conversion, it worked. So basically this gem breaks by default if there is an Account or Transfer model already present, and in addition to that breaks in other places in ways I'm struggling to understand.

On the primary app I'm using I've commented out my User, Account and Transfer models... and even tried to switch the main scope over to a model to something else more neutral, and it still fails.

Is there a way we can pull this scoping out of double_entry?

Edit: Whoops. Stupid mistake: Turns out my double_entry.rb initializer was in config/ not config/initializer
And with that fix I'm no longer getting a DoubleEntry::UnknownAccount error anymore, caused obviously now by my mistake with the initializer.

So I'll close this issue.

However, I'm now just getting variations of DoubleEntry::AccountScopeMismatchError instead, which I believe relates to #115 so I will post details about that one there.