We have a certain problem with polymorphic relation in rails/activerecord. As mentioned in an other question . The reason we need this kind of polymorphic relation with a integer foreign_type column is the number of records in table, we have about 40 million record in that table an the number is raising. We try to save storage at the database server an memory consumption concerning the index handling at the database.
The question mentioned earlier is related to Rails 2 and if already tried to uses this with Rails 3 but it doesn't work. The method of the module was never called and i can't see why.
I would like to have a mapping like this with column types see in the migration class
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
I would like to map Comment and User with a numeric value and save the numeric value instead of the class name as type information.