我有TestVisual
类继承的Game
类:
class TestVisual < Game
include MongoMapper::Document
end
class Game
include MongoMapper::Document
belongs_to :maestra
key :incorrect, Integer
key :correct, Integer
key :time_to_complete, Integer
key :maestra_id, ObjectId
timestamps!
end
你可以看到它属于Maestra。
所以我可以Maestra.first.games
返回[]
但我不能Maestra.first.test_visuals
,因为它会返回undefined method test_visuals
由于我专注于TestVisuals
,理想情况下这是我想要的,但仍然让它共享其父级Game类的属性。
Mongo可以实现吗?如果不是或者没有必要,是否还有其他更好的方法从Maestra到达TestVisual对象并仍然继承游戏?
答案 0 :(得分:1)
MongoMapper中的单一集合继承(SCI)自动生成选择, 例如,以下产生相同的结果。
p Game.where(_type: 'TestVisual').all
p TestVisual.all
另见mongomapper / lib / mongo_mapper / plugins / sci.rb - MongoMapper :: Plugins :: Sci :: ClassMethods #query
但是,MongoMapper不会根据基类的关联自动生成子类的关联, 而我认为不应该这样做。
请注意,SCI将子类和基类放在同一个MongoDB集合中。 如果这不是您想要的,您应该考虑其他模块化机制。
您可以自己为关联访问器方法定义以下方法,也许这足以满足您的需要? 对于其他关联方法,如append或push,父方法可能是可行的。
class Maestra
include MongoMapper::Document
key :name, String
many :games
def test_visuals
games.where(_type: 'TestVisual')
end
end
测试/单元/ test_visual_test.rb
require 'test_helper'
def ppp(obj)
puts obj.inspect.gsub(/, ([^#])/, ",\n\t\\1").gsub(/, #/, ",\n #")
end
class TestVisualTest < ActiveSupport::TestCase
def setup
Maestra.delete_all
Game.delete_all
end
test "inheritance" do
maestra = Maestra.create(name: 'Fiona')
maestra.games << Game.create(incorrect: 1, correct: 9, time_to_complete: 60)
maestra.games << TestVisual.create(incorrect: 2, correct: 8, time_to_complete: 61)
ppp maestra.games.to_a
ppp maestra.test_visuals.to_a
end
end
输出
Run options: --name=test_inheritance
# Running tests:
[#<Game _id: BSON::ObjectId('4ff7029a7f11ba6e43000002'),
_type: "Game",
correct: 9,
created_at: Fri,
06 Jul 2012 15:22:02 UTC +00:00,
incorrect: 1,
maestra_id: BSON::ObjectId('4ff7029a7f11ba6e43000001'),
time_to_complete: 60,
updated_at: Fri,
06 Jul 2012 15:22:02 UTC +00:00>,
#<TestVisual _id: BSON::ObjectId('4ff7029a7f11ba6e43000003'),
_type: "TestVisual",
correct: 8,
created_at: Fri,
06 Jul 2012 15:22:02 UTC +00:00,
incorrect: 2,
maestra_id: BSON::ObjectId('4ff7029a7f11ba6e43000001'),
time_to_complete: 61,
updated_at: Fri,
06 Jul 2012 15:22:02 UTC +00:00>]
[#<TestVisual _id: BSON::ObjectId('4ff7029a7f11ba6e43000003'),
_type: "TestVisual",
correct: 8,
created_at: Fri,
06 Jul 2012 15:22:02 UTC +00:00,
incorrect: 2,
maestra_id: BSON::ObjectId('4ff7029a7f11ba6e43000001'),
time_to_complete: 61,
updated_at: Fri,
06 Jul 2012 15:22:02 UTC +00:00>]
.
Finished tests in 0.026661s, 37.5080 tests/s, 0.0000 assertions/s.
1 tests, 0 assertions, 0 failures, 0 errors, 0 skips