1

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

它工作正常继承,但我也想能够有其他类belongs_to它。

这可能吗? IE下面的东西。是否有一些方法可以用来为子类提供一个键?

https://github.com/hexorx/countries

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

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

目前,我得到NoM​​ethodError:未定义的方法`的has_many”的国家:类

或一些办法,包括/初始化对象后,继承了宝石的属性?

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 

回答

0

对我来说,最好的行为是不使用你的情况has_many但创建要与Mongoid查询里面的方法。