我想使用我在这里找到的国家宝石,而不是创建一个单独的模型。
它可以很好地继承,但我也希望能够有其他类belong_to它。
这个是可能的吗?如下图所示。有没有什么方法可以用来为子类提供一个键?
https://github.com/hexorx/countries
class Country < ISO3166::Country
#include Mongoid::Document
#RELATIONS
has_many :cities
has_many :reviews, as: :reviewable
end目前,我得到了Country:`has_many的NoMethodError:未定义方法类
或者在对象初始化后通过某种方式从gem中包含/继承属性?
class Country# < ISO3166::Country
include Mongoid::Document
#field :name, :type => String
field :country_id, :type => String
##RELATIONS
has_many :cities
has_many :reviews, as: :reviewable
def after_initialize
ISO3166::Country.find_country_by_alpha3(self.country_id)
end
end发布于 2012-03-23 16:51:49
对我来说,最好的行为不是在你的例子中使用has_many,而是创建你想要的方法,里面有Mongoid查询。
https://stackoverflow.com/questions/9812183
复制相似问题