我可以通過活動記錄belongs_to使用hstore哈希中的字段將模型綁定到另一個模型嗎?我會詳細說明:我可以使用ActiveRecord與Hstore的字段關係嗎?
我有一個被通過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到我的模型:
的Gemfile:
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
所以你期望的軌道拉相應Nightclub.find(1)調用「NightclubBoss.nightclub時綁定到這個模型「, 對?那麼,它不會發生:
[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
我認爲這也可能是一個非常糟糕的做法要遵循。如果有更好的方法來模擬我需要的信息,請告訴我,我會感謝您的建議。謝謝!
你有沒有爲班級夜總會定義'has_one:nightclub_boss'協會? –
很好的@FredWillmore,我愚蠢地在夜總會有一個belongs_to:nightclub_boss。 但是我改成了has_one:nightclub_boss,甚至has_one:nightclub_boss,class_name:「NightclubBoss」,仍然沒有骰子......我認爲這仍然與從json拉出場地造成的怪異有關。 –