Rails 请教 table_name_prefix 的用法

laocainiao · 2021年12月28日 · 最后由 laocainiao 回复于 2021年12月28日 · 318 次阅读

class Post < ApplicationRecord
  self.table_name_prefix = "st_"
  self.table_name = 'posts'
end


Loading development environment (Rails 7.0.0)
irb(main):001:0> Post.table_name
=> "posts"
irb(main):002:0> Post.first
  Post Load (17.5ms)  SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT $1  [["LIMIT", 1]]
irb(main):003:0> Post.reset_table_name
=> "st_posts"

我预期 Post 能正确的给出 我配置好的 数据表名称,并在 sql 查询中应用配置好的数据表名称,因为我想在 migration 中使用可配置的 数据表名称。

相关链接:

去掉 self.table_name = 'posts' 看看,我觉得这行已经覆盖了 prefix。

去掉了可行。看来和我预想的用法不一样。谢谢

laocainiao 关闭了讨论。 12月29日 12:18
需要 登录 后方可回复, 如果你还没有账号请 注册新账号