Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rails Polymorphic associations explained
class User < ActiveRecord::Base
attr_accessible :email, :name
has_many :comments, :as => :commendable
end
class Event < ActiveRecord::Base
attr_accessible :name, :organizer, :place
has_many :comments, :as => :commendable
end
class Comment < ActiveRecord::Base
attr_accessible :comment
belongs_to :commendable, :polymorphic => true
end
# Migration: create_comments.rb
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.text :comment
t.references :commendable, :polymorphic => true
t.timestamps
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.