【问题标题】:rails 3 mongoid and countries gemrails 3 mongoid 和国家宝石
【发布时间】:2012-03-21 20:16:56
【问题描述】:

我想使用我在此处找到的国家 gem,而不是创建一个单独的模型。

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

这可能吗? 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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rubygems mongoid country-codes


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-12-12
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      • 1970-01-01
      相关资源
      最近更新 更多