Models with names causing UndefinedTable error

I have the following models with names:

# app/models/face_data/pool_membership.rb
class FaceData::PoolMembership < ActiveRecord::Base
  self.table_name = 'face_data_pool_memberships'

  belongs_to :pool, class_name: 'FaceData::Pool'
  belongs_to :photo
end

# app/models/face_data/pool.rb
class FaceData::Pool < ActiveRecord::Base
  self.table_name = 'face_data_pools'
end

# app/models/photo.rb
class Photo < ActiveRecord::Base
  has_many :pool_memberships, class_name: 'FaceData::PoolMembership'
  has_many :pools, through: :pool_memberships, class_name: 'FaceData::Pool'
end

And the database schema is as follows:

# db/schema.rb
create_table "face_data_pool_memberships", force: true do |t|
  # omitted
end

create_table "face_data_pools", force: true do |t|
  # omitted
end

The application works fine, but when I boot (server, rake task, etc.) I get the following error:

 PG::UndefinedTable: ERROR:  relation "pool_memberships" does not exist
 LINE 5:                WHERE a.attrelid = '"pool_memberships"'::regc...
 ^
 :               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
 pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
 FROM pg_attribute a LEFT JOIN pg_attrdef d
 ON a.attrelid = d.adrelid AND a.attnum = d.adnum
 WHERE a.attrelid = '"pool_memberships"'::regclass
 AND a.attnum > 0 AND NOT a.attisdropped
 ORDER BY a.attnum

This only happens in production (not in the development environment) and looks as if it does not affect the runtime (the application continues to run finely polarized queries using the correct table name).

Note that the application has other FaceDatamodules and classes with names in the namespace. Setting table_name_prefixexplicitly to an empty line does not cause the error to disappear.

I guess this has something to do with loading file order or association has_many :through. Any tips?

+4
2

, . app/models/face_data.rb

    module FaceData
      def self.table_name_prefix
        'face_data_'
      end
    end

self.table_name , . , .

+11

, , .

Rails 4 /config/application.rb, , , module FaceData.

Rails 3, , .

0

Source: https://habr.com/ru/post/1533512/


All Articles