https://github.com/mansakondo/polymorphic_aliases
An ActiveRecord extension to use aliases for your polymorphic associations
https://github.com/mansakondo/polymorphic_aliases
activerecord polymorphism rails ruby
Last synced: 10 months ago
JSON representation
An ActiveRecord extension to use aliases for your polymorphic associations
- Host: GitHub
- URL: https://github.com/mansakondo/polymorphic_aliases
- Owner: mansakondo
- License: mit
- Created: 2021-07-21T12:28:09.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-06-12T19:58:29.000Z (about 4 years ago)
- Last Synced: 2025-08-09T20:55:39.037Z (11 months ago)
- Topics: activerecord, polymorphism, rails, ruby
- Language: Ruby
- Homepage:
- Size: 52.7 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# polymorphic_aliases
An ActiveRecord extension to use aliases for your polymorphic associations.
## Usage
Your model code:
```ruby
# 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:
```ruby
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:
```ruby
gem "polymorphic_aliases"
```
Or if you want to target the `main` branch:
```ruby
gem "polymorphic_aliases", github: "mansakondo/polymorphic_aliases"
```
And then execute:
```bash
$ bundle
```
Or install it yourself as:
```bash
$ gem install polymorphic_aliases
```
## Contributing
Contribution directions go here.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).