首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails 3 mongoid和countries gem

rails 3 mongoid和countries gem
EN

Stack Overflow用户
提问于 2012-03-22 04:16:56
回答 1查看 437关注 0票数 1

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

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

这个是可能的吗?如下图所示。有没有什么方法可以用来为子类提供一个键?

https://github.com/hexorx/countries

代码语言:javascript
复制
class Country < ISO3166::Country
  #include Mongoid::Document    

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

目前,我得到了Country:`has_many的NoMethodError:未定义方法类

或者在对象初始化后通过某种方式从gem中包含/继承属性?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2012-03-23 16:51:49

对我来说,最好的行为不是在你的例子中使用has_many,而是创建你想要的方法,里面有Mongoid查询。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9812183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档