如何在Rails CouchRest模型中添加模块中的属性以支持多重继承?

时间:2011-09-16 00:28:57

标签: ruby-on-rails ruby rails-models couchrest

在我的班上,我想要包含多个模块。每个模块都可以 定义自己的属性以在couchDB中保留。

以下是一个例子:

module Owner
 property :name
end

module Animal
 property :type
end

class Cat
 include Owner
 include Animal
end

这不起作用。我收到了这个错误:“未定义的方法`属性'”。 我尝试添加CouchRest :: Model :: Embeddable但它不适用 模块要么。我看到的所有例子都在延伸 CouchRest ::型号::基地。但是,我将无法使用这种方法 因为Ruby不支持多重继承。

我将无法更改基础JSON格式。我想要的格式是{“name”:“tom”,“type”:“cat”}。

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

根据http://www.couchrest.info/model/embedding.html我认为你的例子是:

class Owner < CouchRest::Model::Base
 include CouchRest::Model::Embeddable
 property :name
end

class Animal < CouchRest::Model::Base
 include CouchRest::Model::Embeddable
 property :type
end

class Cat
 property :owner, Owner
 property :animal, Animal
end