为多个模型创建一个界面?

Create one interface for many models?

我目前正在 Rails 上开发一个用 Ruby 编写的自定义商店软件,其中包含三个 gift/voucher 模型。一种用于百分比折扣,一种用于特定金额,一种用于团体。现在需要对其进行大修,我正在考虑创建一个界面,与旧模型具有多态关系的模型或类似的东西来为所有三个模型添加特性和功能。

你会怎么处理?我想我会选择具有多态关系的模型。有什么建议weclome!

您可以使用模块(这可能会扩展问题)。

How to use concerns in Rails 4

module Giftable
    extend ActiveSupport::Concern

    included do 
       # add relationships, validations etc.
    end

    def some_instance_method
       ..
    end

    module ClassMethods     
        def some_class_method
        ..
        end
    end 
end

gift/voucher 型号之一。

class SimpleVoucher < ActiveRecord::Base    
    include Giftable
end