Repository is archived
Low commit activity in last 3 years
No release in over a year
Define your model associations in the database without changing the schema or models.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Rails Dynamic Associations

Define your model associations in the database without changing the schema or models.

DEPRECATED

Use Adjustable Schema instead. It has a similar API, though it's not backward-compatible.

Features

  • Creates associations for your models when application starts.
  • Provides Relation & Role models.
  • No configuration code needed.
  • No code generated or inserted to your app (except migrations).
  • Adds some useful methods to ActiveRecord objects to handle their relations.

Installation

  1. Add the gem to your Gemfile and bundle it.
  2. Copy migrations to your app (rake rails_dynamic_associations:install:migrations).
  3. Migrate the DB (rake db:migrate).

Usage

Add configuration records to the DB:

	Relation.create({
		source_type: Person,
		target_type: Book,
	})

Or use a helper method:

	Relation.seed Person, Book

Now you have:

	person.books
	book.people

Roles

You can create multiple role-based associations between two models.

	Relation.seed Person, Book, %w[
		author
		editor
	]

You will get:

	person.books
	person.authored_books
	person.edited_books

	book.people
	book.author_people
	book.editor_people

Special cases

In case you have set up relations with a User model you'll get a slightly different naming:

	Relation.seed User, Book, %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.

TODO
  • Describe self-referential associations.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request