vendredi 7 juillet 2017

ActiveAdmin looking up wrong table names for nested modules and ActiveRecord models

I have a module where all our Accounting ActiveRecord classes live within. I have an AR class called Requests and a subclass called Check (logically there will be multiple types of requests for accounting)

In my test suite for Accounting::Requests::Check I have the following tests:

subject(:check) { build(:check_with_items) } # Uses FactoryGirl

it "should have the right table name" do
   expect(Accounting::Requests::Check.table_name).to eq('accounting_request_checks')
end

it "should be a valid factory instance" do
  expect(check).to be_valid
end

Which pass and are correct.

However, when I do the following within an ActiveAdmin.register_page block:

ActiveAdmin.register_page 'Accounting Requests' do
  ... menu stuff

  controller do
    def index
      @check = Accounting::Requests::Check.new
    end

I get the this error:

PG::UndefinedTable: ERROR:  relation "accounting_checks" does not exist

My test ensures that this model has the right table name, being "accounting_request_checks", yet AA is looking for "accounting_checks".

Directory structure:

/models
   /accounting
     request.rb
     /requests
       check.rb  

Here are the class declarations:

class Accounting::Requests < ActiveRecord::Base

class Accounting::Requests::Check < ActiveRecord::Base

Why is active admin looking up the incorrect table name?

I've also tried changing requests from a class to a module with a function using self.table_name_prefix (that's a Rails 3.1 method)


update:

It's not ActiveAdmin - in rails console doing Accounting::Requests::Check.new gives the same error.

Aucun commentaire:

Enregistrer un commentaire