No release in over a year
An ActiveRecord extension to use aliases for your polymorphic associations.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 6.0
 Project Readme

polymorphic_aliases

An ActiveRecord extension to use aliases for your polymorphic associations.

Usage

Your model code:

# app/models/comment.rb    
class Comment < ApplicationRecord    
  belongs_to :commentable, polymorphic: true, types: %w( Post Picture )    
end    
    
# app/models/post.rb    
class Post < ApplicationRecord    
  has_many :comments, as: :commentable    
end    
    
# app/models/picture.rb    
class Picture < ApplicationRecord    
  has_many :comments, as: :commentable    
end

In the console:

post    = Post.create(title: "Polymorphic Aliases", content: "...")
picture = Picture.create(legend: "Here's my code.", url: "https://avatars.githubusercontent.com/u/47113995?v=4")

comment_from_post    = Comment.create(content: "...", commentable: post)
comment_from_picture = Comment.create(content: "...", commentable: picture)

comment_from_post.post.title
# => "Polymorphic Aliases"

comment_from_picture.picture.legend
# => "Here's my code."

comment_from_post.picture
# => nil

comment_from_picture.post
# => nil

Installation

Add this line to your application's Gemfile:

gem "polymorphic_aliases"

Or if you want to target the main branch:

gem "polymorphic_aliases", github: "mansakondo/polymorphic_aliases"

And then execute:

$ bundle

Or install it yourself as:

$ gem install polymorphic_aliases

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.