2

モデル化の方法がよくわからない状況に遭遇しました。

編集: 以下のコードは現在、実用的なソリューションを表しています。ただし、見栄えの良いソリューションにはまだ興味があります。

User クラスがあり、ユーザーが多くのサービスを持っているとします。ただし、これらのサービスは aMailServiceと aBackupServiceのようにかなり異なるため、単一のテーブル継承では機能しません。代わりに、抽象基本クラスと一緒にポリモーフィックな関連付けを使用することを考えています。

class User < ActiveRecord::Base
    has_many :services
end

class Service < ActiveRecord::Base
    validates_presence_of :user_id, :implementation_id, :implementation_type
    validates_uniqueness_of :user_id, :scope => :implementation_type

    belongs_to :user
    belongs_to :implementation, :polymorphic => true, :dependent => :destroy
    delegate :common_service_method, :name, :to => :implementation
end

#Base class for service implementations
    class ServiceImplementation < ActiveRecord::Base
    validates_presence_of :user_id, :on => :create

    #Virtual attribute, allows us to create service implementations in one step
    attr_accessor :user_id
    has_one :service, :as => :implementation

    after_create :create_service_record

    #Tell Rails this class does not use a table.
    def self.abstract_class?
        true
    end

    #Name of the service.
    def name
        self.class.name
    end

    #Returns the user this service
    #implementation belongs to.
    def user
        unless service.nil? 
            service.user
        else #Service not yet created
            @my_user ||= User.find(user_id) rescue nil
        end
    end

    #Sets the user this 
    #implementation belongs to.
    def user=(usr)
        @my_user = usr
        user_id = usr.id
    end

    protected

    #Sets up a service object after object creation.
    def create_service_record
        service = Service.new(:user_id => user_id)
        service.implementation = self
        service.save!
    end
end
class MailService < ServiceImplementation
   #validations, etc...
   def common_service_method
     puts "MailService implementation of common service method"
   end
end

#Example usage
MailService.create(..., :user => user)
BackupService.create(...., :user => user)

user.services.each do |s|
    puts "#{user.name} is using #{s.name}"
end #Daniel is using MailService, Daniel is using BackupService

新しいサービスを作成するときに Service インスタンスを暗黙的に作成することに注意してください。

それで、これが最善の解決策ですか?それとも良いものですか?この種の問題をどのように解決しましたか?

4

2 に答える 2

3

あなたの現在の解決策はうまくいかないと思います。ServiceImplementation が抽象の場合、関連するクラスは何を指しますか? ServiceImplementation がデータベースに永続化された pk を持っていない場合、has_one の反対側はどのように機能しますか? 多分私は何かが足りない。

編集: おっと、私のオリジナルも機能しませんでした。しかし、アイデアはまだそこにあります。モジュールの代わりに、ポリモーフィズムの代わりに STI を使用して Service を使用し、個々の実装でそれを拡張します。さまざまな実装で STI と多数の未使用の列に行き詰まっているか、一般的なサービス関係を再考していると思います。あなたが持っている委任ソリューションは、別の ActiveRecord として機能する可能性がありますが、has_one 関係が必要な場合、抽象としてどのように機能するかわかりません。

編集:元の抽象的なソリューションの代わりに、デリゲートを永続化してみませんか? MailServiceDelegate と BackupServiceDelegate 用に別々のテーブルを用意する必要があります。純粋な STI ですべての null 列を回避したい場合、それを回避する方法がわかりません。デリゲート クラスを含むモジュールを使用して、一般的な関係や検証などをキャプチャできます。申し訳ありませんが、問題に追いつくのに数回かかりました。

class User < ActiveRecord::Base
  has_many :services
end

class Service < ActiveRecord::Base
  validates_presence_of :user_id
  belongs_to :user
  belongs_to :service_delegate, :polymorphic => true
  delegate :common_service_method, :name, :to => :service_delegate
end

class MailServiceDelegate < ActiveRecord::Base
   include ServiceDelegate

   def name
     # implement
   end

   def common_service_method
      # implement
   end
end

class BackupServiceDelegate < ActiveRecord::Base
   include ServiceDelegate

   def name
     # implement
   end

   def common_service_method
      # implement
   end
end

module ServiceDelegate
   def self.included(base)
     base.has_one :service, :as => service_delegate
   end

   def name
     raise "Not Implemented"
   end

   def common_service_method
     raise "Not Implemented"
   end
end
于 2010-06-02T17:07:09.097 に答える
0

私は以下がうまくいくと思います

user.rbで

  has_many :mail_service, :class_name => 'Service'
  has_many :backup_service, :class_name => 'Service'

service.rbで

  belongs_to :mail_user, :class_name => 'User', :foreign_key => 'user_id', :conditions=> is_mail=true
  belongs_to :backup_user, :class_name => 'User', :foreign_key => 'user_id', :conditions=> is_mail=false
于 2010-06-02T10:51:01.240 に答える