Keep getting PG :: UndefinedTable with the model named

I have some models created using namespacing.

rails g model Technology::Post ...

This led to an error

PG::UndefinedTable: ERROR:  relation "posts" does not exist
LINE 5:                WHERE a.attrelid = '"posts"'::regclass
               :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 = '"posts"'::regclass
             AND a.attnum > 0 AND NOT a.attisdropped
           ORDER BY a.attnum

Since they have names, I have a model technology.rb

module Technology
  def self.table_name_prefix
    'technology_'
  end
end

And, of course, a model Technology::Post. This model is located in a folder technologyand is called post.rb.

class Technology::Post < ActiveRecord::Base
  ...
end

My database name technology_posts.

public | technology_posts                  | table | postgres

However, for some reason he is looking for a table posts. In the controller, I can just create the base @posts = Technology::Post.allone and it will happen anyway.

Restarting the server will fix the problem until it repeats. This is also not a problem in our production environment. Only development.

+4
source share
2 answers

:

self.table_name = 'NAME OF YOUR TABLE'
+1

, .

, ​​ pg.

Udit, , . . , , , :

, Rails :

module Technology
  def self.table_name_prefix
    'technology_'
  end
end

, / "" , Rails ! , puts .

, , , , . ,

module Technology
  def self.table_name_prefix
    'technology_'
  end
  class Post < ActiveRecord::Base
    ...
  end
end
+1

All Articles