简体   繁体   English

rails 3 mongoid和国家/地区宝石

[英]rails 3 mongoid and countries gem

I'd like to use the countries gem I found here instead creating a separate model. 我想使用在这里找到的国家/地区宝石,而不是创建一个单独的模型。

It works fine to inherit from but I'd also like to be able to have other classes belong_to it. 从它继承可以很好地工作,但我也希望能够有其他类归属于它。

Is this possible? 这可能吗? IE something like below. IE浏览器如下。 Is there some method I could use to provide a key to child classes? 我可以使用某些方法来提供子类的键吗?

https://github.com/hexorx/countries https://github.com/hexorx/countries

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

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

At the moment I get NoMethodError: undefined method `has_many' for Country:Class 此刻我得到NoM​​ethodError:Country:Class的未定义方法“ has_many”

Or some way to include/inherit the attributes from the gem after the object is initialized? 还是在对象初始化后通过某种方式包含/继承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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM