Rails 请教 table_name_prefix 的用法

laocainiao · December 28, 2021 · Last by laocainiao replied at December 28, 2021 · 318 hits

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 closed this topic. 29 Dec 12:18
You need to Sign in before reply, if you don't have an account, please Sign up first.