我有一系列模型都继承自基本模型Properties
例如Bars, Restaurants, Cafes, etc.
class Property
include MongoMapper::Document
key :name, String
key :_type, String
end
class Bar < Property
我想知道的是,当唱片恰好是酒吧和餐厅时,该怎么办?有没有办法让单个对象继承两个模型的属性?以及它如何与密钥 :_type 一起使用?
我有一系列模型都继承自基本模型Properties
例如Bars, Restaurants, Cafes, etc.
class Property
include MongoMapper::Document
key :name, String
key :_type, String
end
class Bar < Property
我想知道的是,当唱片恰好是酒吧和餐厅时,该怎么办?有没有办法让单个对象继承两个模型的属性?以及它如何与密钥 :_type 一起使用?
我想你想要一个模块在这里。
class Property
include MongoMapper::Document
key :name, String
key :_type, String
end
module Restaurant
def serve_food
puts 'Yum!'
end
end
class Bar < Property
include Restaurant
end
Bar.new.serve_food # => Yum!
这样,您可以让许多模型具有餐厅的属性,而无需重复您的代码。
尽管我自己没有尝试过,但您也可以尝试多级继承。例如:
class Property
include MongoMapper::Document
key :name, String
key :_type, String
end
class Restaurant < Property
key :food_menu, Hash
end
class Bar < Restaurant
key :drinks_menu, Hash
end
我不确定 MongoMapper 是否支持这一点,但我不明白为什么它不支持。