我有以下命名空间模型:
# 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
数据库架构如下:
# 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
应用程序正常运行但在启动时(服务器,rake任务等)我收到以下错误:
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
它只发生在生产中(不在开发环境中),看起来它在运行时没有效果(应用程序继续运行良好 - 生成的查询使用正确的表名)。
请注意,应用程序中还有其他FaceData
命名空间模块和类。将table_name_prefix
显式设置为空字符串不会使错误消失。
我的猜测是它与加载文件顺序或has_many :through
关联有关。有什么提示吗?
答案 0 :(得分:11)
为了避免所有这些问题,你可以做一件事。使用这些内容创建app/models/face_data.rb
module FaceData
def self.table_name_prefix
'face_data_'
end
end
通过这种方式,您不再需要为在同一范围内定义的每个类编写self.table_name
。如果您有任何疑问,请告诉我。
答案 1 :(得分:0)
此错误的另一个原因是您的应用程序名称与您用于模型的名称空间相同。
在Rails 4应用中,您只需更新/**
* Created on 8/28/15.
* Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach.
* Chapter 2 Self-Check Problems
*/
public class Ch2_SelfCheckProblems {
public static void main(String[] args) {
question34();
}
public static void question34(){
/**
* Table
* Line 1 ! = 22 \ = 0 / = 0
* Line 2 ! = 18 \ = 2 / = 2
* Line 3 ! = 14 \ = 4 / = 4
* Line 4 ! = 10 \ = 6 / = 6
* Line 5 ! = 6 \ = 8 / = 8
* Line 6 ! = 2 \ = 10 / = 10
*/
for (int line = 1; line <= 6; line++){
for (int i = 1; i <= 22; i++){
// for (int j = 1; j <= (line - 1); j++){
// System.out.print("\");
// }
System.out.print("!");
}
System.out.println();
}
}
}
文件即可重命名其中定义的模块,例如/config/application.rb
。
在Rails 3应用程序中,我相信您还需要更新路径文件。