Attr_accessor和子类

时间:2014-09-08 15:09:31

标签: ruby-on-rails ruby-on-rails-4 attr-accessor

我有User模型和两个子类Developer和Buyer(子模型)。在每个模型中,我都有不变的FIELDS。

在型号用户

class User < ActiveRecord::Base
  attr_accessor *:get_fields

  after_initialize :get_additional_fields

  # FIELDS for current class
    def self.get_fields
      Object.const_get( self.name )::FIELDS
    end

  # set additional fields
  #
    def get_additional_fields
      self.country = 'text'
    end
end

我有一些错误NoMethodError: undefined methodcountry=' for #<Developer:

我的错误是什么?

更新

class Buyer < User
  FIELDS = [ :country, :avatar ]
end

class Developer < User
  FIELDS = [ :country, :avatar, :location, :first_name, :last_name ]
end

我的请求d = Developer.first

0 个答案:

没有答案