1

我想使用我在这里找到的国家宝石而不是创建一个单独的模型。

它可以很好地继承,但我也希望能够让其他类属于它。

这可能吗?IE 如下所示。有什么方法可以用来为子类提供密钥吗?

https://github.com/hexorx/countries

class Country < ISO3166::Country
  #include Mongoid::Document    

  #RELATIONS
  has_many :cities
  has_many :reviews, as: :reviewable
end

目前我得到 NoMethodError: undefined method `has_many' for Country:Class

或者在初始化对象后以某种方式包含/继承 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
4

1 回答 1

0

对我来说,最好的行为不是has_many在您的情况下使用,而是在内部使用 Mongoid 查询创建您想要的方法。

于 2012-03-23T08:51:49.393 回答