gpt4 book ai didi

ruby-on-rails - 我可以将 ActiveRecord 关系与 Hstore 中的字段一起使用吗?

转载 作者:行者123 更新时间:2023-12-04 17:59:57 24 4
gpt4 key购买 nike

我可以使用 hstore 散列中的字段通过事件记录 belongs_to 将一个模型绑定(bind)到另一个模型吗?我会详细说明:

我有一个用户模型,它通过 STI 在其字段之一上根据权限被子类化为许多不同的其他用户模型:

class User < ActiveRecord::Base
self.inheritance_column = :role
#other definitions and validations
end

这是一个这样的子模型,nightclub_boss 模型,适用于我的应用程序的管理用户:

class NightclubBoss < User
belongs_to :nightclub #the boss record has a nightclub_id
has_one :rp_boss #rp_boss should have a nightclub_boss_id
has_one :captain #captain should have a nightclub_boss_id
end

这里的想法是使用以下信息扩展用户模型:

-User hierarchy (which user reports to who; get a list of who is under who as needed through the ORM)
-User origin (which user belongs to whatever other models are in the app)

所以我想到的是,为了避免拥有稀疏的 User 表,也为了避免制作大量的部分小 SQL 表,是在 User 模型上创建一个名为“hierarchy”的 hstore 字段,并将其映射到不同的属性当且仅当相关用户需要存储层级信息(即,基于角色;nightclub_boss 不应与其他模型具有相同的层级信息)的用户模型在我的应用程序中):

class AddHstore < ActiveRecord::Migration
def self.up
enable_extension "hstore"
end

def self.down
disable_extension "hstore"
end
end

class AddHierarchyToUser < ActiveRecord::Migration
def change
add_column :users, :hierarchy, :hstore
add_index :users, :hierarchy, using: :gin
end
end

rake db:migrate

然后我将 hstore_accesssor 添加到我的模型中:

gem 文件:

gem "hstore_accessor"

用户模型:

class User < ActiveRecord::Base
self.inheritance_column = :role
hstore_accessor :hierarchy, nightclub_id: :integer
#other validations and definitions...
end

到目前为止一切都很好,使用硬编码数据进行测试:

[1] pry(main)> NightclubBoss.find(99)
NightclubBoss Load (1.3ms) SELECT "users".* FROM "users" WHERE "users"."role" IN ('NightclubBoss') AND "users"."id" = $1 LIMIT 1 [["id", 99]]
=> #<NightclubBoss:0x000000070bbb00
id: 99,
#...all other fields...
role: "NightclubBoss",
hierarchy: {"nightclub_id"=>"1"}>

[2] pry(main)> NightclubBoss.find(99).nightclub_id
NightclubBoss Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."role" IN ('NightclubBoss') AND "users"."id" = $1 LIMIT 1 [["id", 99]]
=> 1

所以您希望 Rails 在调用“NightclubBoss.nightclub”时拉取绑定(bind)到此模型的相应 Nightclub.find(1),对吗?好吧,这不会发生:

[3] pry(main)> NightclubBoss.find(99).nightclub
NightclubBoss Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."role" IN ('NightclubBoss') AND "users"."id" = $1 LIMIT 1 [["id", 99]]
=> nil

我已经尝试了各种方法来解决这个问题,但我还没有找到答案,有没有人可以提供帮助?

作为一种解决方法,我意识到我可以这样做:

Nightclub.find(NightclubBoss.find(99).nightclub_id)

我得到了很好的查询。但我认为这可以改进,你希望能够做到 NightclubBoss.find(99).nightclub

我认为这也可能是一个非常糟糕的做法。如果有更好的方法来模拟我需要的那种信息,请告诉我,我会很感激你的建议。谢谢!

最佳答案

我一夜之间发现 Postgres 有一个名为 Jsonb 的类似数据类型,它也做与 hstore 类似的事情,即你将哈希存储在关系数据库的一个字段中。

显然,由于 ActiveRecord 的限制,目前不可能进行此匹配。它仅将匹配限制为关系数据库字段:

PostgreSQL jsonb field in ActiveRecord belongs_to association

就我个人而言,我对这个答案感到满意,所以如果没有人提供更多信息,我将关闭此线程,并且我将修改我的架构以不依赖于哈希字段。

想知道是否可以修补支持?

关于ruby-on-rails - 我可以将 ActiveRecord 关系与 Hstore 中的字段一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36704936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com