我们在rails/activerecord中存在多态关系的问题。 正如其他question中所述。 我们需要这种带整数foreign_type列的多态关系的原因是表中的记录数,我们在该表中有大约4000万条记录正在提高。我们试图在数据库服务器上保存与存储索引处理相关的内存消耗。Rails 3与foreign_type列的多态关联作为整数
前面提到的问题与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
我想映射注释和用户使用数字值并保存数值而不是类n作为类型信息。
真的吗?将字符串中的“类型”列更改为整数是成功还是失败?你有没有分析过这个? – messick 2013-05-14 06:26:18
最大的问题是*为什么*你想这样做?类型列是一个字符串的原因。你为什么要它返回一个整数?为什么只是客人? – 2013-05-14 06:32:03
@TarynEast这只是一个例子。我们已经建立了一个社交社区,并将任何触发器与通知表关联起来,这些通知表可能是用户,活动,评论,参与,观察,私人消息等等。目前,通知或新闻的数量约为4千万。 – motionless 2013-05-14 08:24:43