3

rails/activerecord のポリモーフィック リレーションに問題があります。別の質問で述べたように。整数のforeign_type列とのこの種の多態的な関係が必要な理由は、テーブル内のレコード数です。そのテーブルには約4000万のレコードがあり、その数は増えています。データベースでのインデックス処理に関連して、データベース サーバーでのストレージのメモリ消費量を節約しようとします。

前述の質問は Rails 2 に関連しており、すでに Rails 3 でこれを使用しようとしている場合は機能しません。モジュールのメソッドが呼び出されたことがなく、その理由がわかりません。

移行クラスに表示される列タイプを使用して、このようなマッピングを行いたいと思います

class Notification < ActiveRecord::Base
  belongs_to :notifiable, :polymorphic => true
end
class User < ActiveRecord::Base
  attr_accessible :name
  has_many :notifications, :as => :notifiable
end
class Comment < ActiveRecord::Base
  attr_accessible :text
  has_many :notifications, :as => :notifiable
end
class Message < ActiveRecord::Base
  attr_accessible :title, :text
  has_many :notifications, :as => :notifiable
end
class Activity < ActiveRecord::Base
  attr_accessible :title, :description
  has_many :notifications, :as => :notifiable
end
class CreateNotification < ActiveRecord::Migration
  def change
    create_table :notifications do |t|
      t.integer :notifiable_id
      t.integer  :notifiable_type # should be a tinyint at the database
      t.timestamps
    end
  end
end

Comment と User を数値でマッピングし、クラス名の代わりに数値を型情報として保存したいと考えています。

4

2 に答える 2