我有一个继承另一个模型的模型,这个特定的模型不使用我指定的表,而是默认返回到尝试查找该表的父类。
父类位于gem中,子类位于引擎中,其名称空间是隔离的。
父级(在宝石中):
require_relative 'concerns/user_concerns'
require 'bcrypt'
module CoreModels
module Models
class User < ActiveRecord::Base
self.abstract_class = true
extend FriendlyId
friendly_id :first_name, use: [:slugged, :finders, :history]
before_save :encrypt_password
has_many :group_memberships, :dependent => :delete_all
has_many :groups, :through => :group_memberships, :dependent => :delete_all
has_many :roles, :through => :group_memberships, :dependent => :delete_all
has_many :api_keys
validates :first_name, presence: true
validates :user_name, uniqueness: true, presence: true, length: {minimum: 5}
validates :email, presence: true, confirmation: true, uniqueness: true
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
validates :password, presence: true, confirmation: true, length: { minimum: 10 }, if: :new_record?
include CoreModels::Models::Concerns::UserConcerns
before_create{ generate_token(:auth_token) }
def self.authenticate_user(user_name, password)
user = Xaaron::User.find_by_user_name(user_name)
if(user && (user.password == BCrypt::Engine.hash_secret(password, user.salt)))
user
else
nil
end
end
def encrypt_password
if password.present?
self.salt = BCrypt::Engine.generate_salt
self.password = BCrypt::Engine.hash_secret(password, salt)
end
end
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_timestamp = Time.zone.now
save!
UserMailer.password_reset(self).deliver
end
protected
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
end
end
end
儿童(在引擎中):
require 'core_models/models/user'
module Xaaron
class User < CoreModels::Models::User
self.table_name = 'xaaron_users'
end
end
当我运行我的测试时,其中135次失败一遍又一遍地给我同样的错误:
Failure/Error: setup_group_role_permissions_relations_for_administrator
ActiveRecord::StatementInvalid:
PG::UndefinedTable: ERROR: relation "users" does not exist
LINE 5: WHERE a.attrelid = '"users"'::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 = '"users"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
这种特殊方法是:
def setup_group_role_permissions_relations_for_administrator
@user = FactoryGirl.create(:user)
@role = FactoryGirl.create(:admin_role)
@group = FactoryGirl.create(:administrator_group)
@permission = FactoryGirl.create(:can_read)
@role.add_permission = @permission.permission_name
@group.add_role = @role.role_name
@user.add_group_membership(@group, @role)
end
所以当我说使用表x时你可以看到它没有听我说话。相反,它试图使用一个不存在的表./我的所有引擎表的名称都用xaaron_
经过一些调查后,核心问题是FactoryGirl,如果我们看一下发生的第一件事:setup_group_role_permissions_relations_for_administrator
我正在做@user = FactoryGirl.create(:user)
,当我用pry来弄清楚那里发生了什么 - 这就是问题,所以让我们来看看这家工厂:
FactoryGirl.define do
sequence :user_email do |n|
"user#{n}@example.com"
end
# Allows for multiple user names
sequence :user_name do |n|
"user#{n}"
end
sequence :permission_name do |n|
"can_read#{n}"
end
sequence :role_name do |n|
"Member#{n}"
end
factory :user, :class => Xaaron::User do
first_name 'Adam'
last_name 'Something'
user_name {generate :user_name}
email {generate :user_email}
password 'somePasswordThat_Is$ecure10!'
end
factory :admin, :class => Xaaron::User do
first_name "Sample Admin"
email "a@gmail.com"
user_name "Admin_User_Name"
password "admin_Password10985"
end
end
工厂女孩创建用户的方式有问题,因为我可以启动rails控制台并执行Xaaron::User.all
,它知道要查看xaaron_users
而不是users
所以self.table_name=""
有效,但由于某种原因,它不与工厂女孩合作。
答案 0 :(得分:1)
通常,Active Record旨在使用单表继承(用于所有子类的记录的相同表)而不是多表继承(子类的不同表。)
也就是说,不是使用self.table_name =
在子类中设置表名,而是可以尝试覆盖table_name
方法,例如。
def table_name
'xaaron_users'
end
并查看是否适用于您的方案。这是mentioned as an alternative in the docs。