Skip to content

Instantly share code, notes, and snippets.

@willnet
Created September 4, 2017 02:49
Show Gist options
  • Save willnet/45900989357385e44e6ea97ea43ec901 to your computer and use it in GitHub Desktop.
Save willnet/45900989357385e44e6ea97ea43ec901 to your computer and use it in GitHub Desktop.
rails template for polymorphic association
generate(:model, 'article')
generate(:model, 'event')
generate(:model, 'comment commentable:references{polymorphic}:index')
rake('db:migrate')
insert_into_file 'app/models/article.rb', "has_many :comments, as: :commentable\n", after: "class Article < ApplicationRecord\n"
insert_into_file 'app/models/event.rb', "has_many :comments, as: :commentable\n", after: "class Event < ApplicationRecord\n"
create_file 'db/seeds.rb' do
<<~SEED
article = Article.create!
article.comments.create!
event = Event.create!
event.comments.create!
SEED
end
rake('db:seed')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment