缺少 Rails table_name_prefix [英] Rails table_name_prefix missing

查看:17
本文介绍了缺少 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆