缺少Rails table_name_prefix [英] Rails table_name_prefix missing
本文介绍了缺少Rails table_name_prefix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下目录结构
models/foo/setting.rb
models/foo.rb
foo.rb内容
module Foo
def self.table_name_prefix
'foo_'
end
end
和setting.rb内容
and setting.rb content
class Foo::Setting < ActiveRecord::Base
end
当我调用Foo::Setting.find…
时,我得到一个错误SQLException: no such table: settings
,它确实是正确的,因为该表名为foo_settings
,因此rails似乎会忽略为模块Foo指定的表前缀.
As soon as I am calling Foo::Setting.find…
I am getting an error SQLException: no such table: settings
which is indeed correct because the table is named foo_settings
so rails seems to ignore the table prefix specified for the module Foo.
我该怎么做,以使rails考虑前缀?
What can I do so that rails considers the prefix?
推荐答案
您已经在模块(Foo)中定义了一个方法.这并不能在该模块中嵌套的类上神奇地定义该方法.
You've define a method inside a module (Foo). This doesn't magically define that method on a class nested in that module.
我会尝试
class Foo < ActiveRecord::Base
self.abstract_class = true
self.table_name_prefix = 'foo_'
end
然后从Foo
class Foo::Setting < Foo
...
end
这篇关于缺少Rails table_name_prefix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文