覆盖父方法并使其成为私有/受保护

时间:2011-08-22 22:53:54

标签: ruby-on-rails ruby activerecord

我正在尝试将create方法设为私有/受保护ActiveRecord模型。我想做这样的事情:

class Product < ActiveRecord::Base
  def self.create(options)
    private
    super(options)

  end
end

因此我无法Product.create(...)。但是,我需要这样做

class Pencil < Product
    def self.create(options)
        options["category"] = "stationary"
        super(options)
    end
end

这样我可以执行此操作Pencil.create(...)。提前谢谢!

1 个答案:

答案 0 :(得分:1)

class Product < ActiveRecord::Base
  class << self
    def create(options)
      super(options)
    end

    private :create
  end
end

class Pencil < Product
  class << self
    def create(options)
      options["category"] = "stationary"
      super(options)
    end
  end
end