Unpolymorph
Foreign key based replacement for polymorphic associations.
Installation
Add this line to your application's Gemfile:
gem 'unpolymorph'
And then execute:
$ bundle
Or install it yourself as:
$ gem install unpolymorph
Usage
class Comment < ActiveRecord::Base
unpolymorph :commentable, %w{ user article post }
end
c = Comment.new c.commentable = User.first c.commentable # User... Comment.possible_commentable?( Post.first ) # true Comment.possible_commentable?( Group.first ) # false
in migration
Comment.update_sql_commentable_check( migration ) # creates SQL check constraint ensuring exactly one of commentable foreign keys is set