【问题标题】:Can I use ActiveRecord relationships with fields from an Hstore?我可以对 Hstore 中的字段使用 ActiveRecord 关系吗?
【发布时间】:2021-06-29 02:11:01
【问题描述】:

我可以使用 hstore 哈希中的字段通过活动记录 belongs_to 将模型绑定到另一个模型吗?我会详细说明:

我有一个用户模型,它通过 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)

所以我想出的是,为了避免有一个稀疏的用户表,也为了避免制作大量的部分小 SQL 表,是在用户模型上创建一个名为“层次结构”的 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 "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”时会拉出绑定到此模型的相应 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

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

【问题讨论】:

  • 您是否为类 Nightclub 定义了 'has_one :nightclub_boss' 关联?
  • 不错@FredWillmore,我愚蠢地在夜总会有一个belongs_to :nightclub_boss。但是我改为 has_one :nightclub_boss,甚至 has_one :nightclub_boss, class_name: "NightclubBoss",仍然没有骰子......我认为这仍然与从 json 中提取字段产生的怪异有关。

标签: ruby-on-rails activerecord hstore sti


【解决方案1】:

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

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

PostgreSQL jsonb field in ActiveRecord belongs_to association

就我个人而言,我对这个答案很满意,所以如果没有人知道更多信息,我会关闭这个帖子,并且我会修改我的架构以不依赖哈希字段。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    相关资源
    最近更新 更多