Define your model associations in the database without changing the schema or models.
This Rails Engine was renamed and refactored from Rails Dynamic Associations.
- Creates associations for your models when application starts.
- Provides
Relationship
&Relationship::Role
models. - No configuration code needed.
- No code generated or inserted to your app (except migrations).
- Adds some useful methods to
ActiveRecord
models to handle their relationships.
Add configuration records to the DB:
AdjustableSchema::Relationship.create! source_type: 'Person',
target_type: 'Book'
Or use a helper method:
AdjustableSchema::Relationship.seed! Person => Book
Now you have:
person.books
book.people
You can create multiple role-based associations between two models.
AdjustableSchema::Relationship.seed! Person => Book, roles: %w[author editor]
You will get:
person.books
person.authored_books
person.edited_books
book.people
book.author_people
book.editor_people
In case you have set up relationships with User
model you'll get a slightly different naming:
AdjustableSchema::Relationship.seed! User => Book, roles: %w[author editor]
book.users
book.authors
book.editors
The list of models to be handled this way can be set with actor_model_names
configuration parameter.
It includes User
by default.
You may want to set up recursive relationships:
AdjustableSchema::Relationship.seed! Person, roles: %w[friend]
In this case you'll get these associations:
person.parents
person.children # for all the children
person.people # for "roleless" children, not friends
person.friends
person.friended_people
If you prefer a different naming over parents
& children
, you can configure it like this:
AdjustableSchema::Engine.configure do
config.names[:associations][:source][:self] = :effect
config.names[:associations][:target][:self] = :cause
end
Thus, for hierarchical Event
s, you'll get:
event.causes
event.effects
Add this line to your application's Gemfile:
gem "adjustable_schema"
And then execute:
bundle
Or install it yourself as:
gem install adjustable_schema
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
The gem is available as open source under the terms of the MIT License.