ActiveRecord extension that automatically (DRY) creates associations based on the schema
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


SchemaAssociations is an ActiveRecord extension that keeps your model class definitions simpler and more DRY, by automatically defining associations based on the database schema.

Gem Version Build Status Coverage Status


One of the great things about Rails (ActiveRecord, in particular) is that it inspects the database and automatically defines accessors for all your columns, keeping your model class definitions simple and DRY. That's great for simple data columns, but where it falls down is when your table contains references to other tables: then the "accessors" you need are the associations defined using belongs_to, has_one, has_many, and has_and_belongs_to_many -- and you need to put them into your model class definitions by hand. In fact, for every relation, you need to define two associations each listing its inverse, such as

class Post < ActiveRecord::Base
    has_many :comments, inverse_of: :post

class Comment < ActiveRecord::Base
    belongs_to :post, inverse_of: :comments

....which isn't so DRY.

Enter the SchemaAssociations gem. It extends ActiveRecord to automatically define the appropriate associations based on foreign key constraints in the database.

SchemaAssociations works particularly well with the schema_auto_foreign_keys gem which automatically defines foreign key constraints. So the common case is simple -- if you have this in your migration:

create_table :posts do |t|

create_table :comments do |t|
    t.integer post_id

Then all you need for your models is:

class Post < ActiveRecord::Base

class Comment < ActiveRecord::Base

and SchemaAssociations defines the appropriate associations under the hood.

What if I want something special?

You're always free to define associations yourself, if for example you want to pass special options. SchemaAssociations won't clobber any existing definitions.

You can also control the behavior with various options, via a global initializer and/or per-model. See the Configuration section for the available options.

This seems cool, but I'm worried about too much automagic

You can globally turn off automatic creation in config/initializers/schema_associations.rb:

SchemaAssociations.setup do |config|
    config.auto_create = false

Then in any model where you want automatic associations, just do

class Post < ActiveRecord::Base

You can also pass options as described in Configuration

Full Details

The basics

The common cases work entirely as you'd expect. For a one-to-many relationship using standard naming conventions:

# migration:
create_table :comments do |t|
    t.integer post_id

# schema_associations defines:
class Post < ActiveRecord::Base
    has_many :comments

class Comment < ActiveReocrd::Base
    belongs_to :post

For a one-to-one relationship:

# migration:
create_table :comments do |t|
    t.integer post_id, index: :unique    # (using the :index option provided by schema_plus )

# schema_associations defines:
class Post < ActiveRecord::Base
    has_one :comment

class Comment < ActiveReocrd::Base
    belongs_to :post

And for many-to-many relationships:

# migration:
create_table :groups_members do |t|
    integer :group_id
    integer :member_id

# schema_associations defines:
class Group < ActiveReocrd::Base
    has_and_belongs_to_many :members

class Member < ActiveRecord::Base
    has_and_belongs_to_many :groups

Unusual names, multiple references

Sometimes you want or need to deviate from the simple naming conventions. In this case, the belongs_to relationship name is taken from the name of the foreign key column, and the has_many or has_one is named by the referencing table, suffixed with "as" the relationship name. An example should make this clear...

Suppose your company hires interns, and each intern is assigned a manager and a mentor, who are regular employees.

create_table :interns do |t|
    t.integer :manager_id,      references: :employees
    t.integer :mentor_id,       references: :employees

SchemaAssociations defines a belongs_to association for each reference, named according to the column:

class Intern < ActiveRecord::Base
    belongs_to  :manager, class_name: "Employee", foreign_key: "manager_id"
    belongs_to  :mentor,  class_name: "Employee", foreign_key: "mentor_id"

And the corresponding has_many association each gets a suffix to indicate which one relation it refers to:

class Employee < ActiveRecord::Base
    has_many :interns_as_manager, class_name: "Intern", foreign_key: "manager_id"
    has_many :interns_as_mentor,  class_name: "Intern", foreign_key: "mentor_id"

Special case for trees

If your forward relation is named "parent", SchemaAssociations names the reverse relation "child" or "children". That is, if you have:

create_table :nodes
    t.integer :parent_id         # schema_plus assumes it's a reference to this table

Then SchemaAssociations will define

class Node < ActiveRecord::Base
    belongs_to :parent, class_name: "Node", foreign_key: "parent_id"
    has_many :children, class_name: "Node", foreign_key: "parent_id"

Concise names

For modularity in your tables and classes, you might use a common prefix for related objects. For example, you may have widgets each of which has a color, and each widget might have one frob that has a top color and a bottom color--all from the same set of colors.

create_table :widget_colors |t|

create_table :widgets do |t|
    t.integer   :widget_color_id

create_table :widget_frobs
    t.integer :widget_id, index: :unique
    t.integer :top_widget_color_id,    references: :widget_colors
    t.integer :bottom_widget_color_id, references: :widget_colors

Using the full name for the associations would make your code verbose and not quite DRY:


Instead, by default, SchemaAssociations uses concise names: shared leading words are removed from the association name. So instead of the above, your code looks like:


i.e. these associations would be defined:

class WidgetColor < ActiveRecord::Base
    has_many :widgets,         class_name: "Widget",     foreign_key: "widget_color_id"
    has_many :frobs_as_top,    class_name: "WidgetFrob", foreign_key: "top_widget_color_id"
    has_many :frobs_as_bottom, class_name: "WidgetFrob", foreign_key: "bottom_widget_color_id"

class Widget < ActiveRecord::Base
    belongs_to :color, class_name: "WidgetColor", foreign_key: "widget_color_id"
    has_one    :frob,  class_name: "WidgetFrob",  foreign_key: "widget_frob_id"

class WidgetFrob < ActiveRecord::Base
    belongs_to :top_color,    class_name: "WidgetColor", foreign_key: "top_widget_color_id"
    belongs_to :bottom_color, class_name: "WidgetColor", foreign_key: "bottom_widget_color_id"
    belongs_to :widget,       class_name: "Widget",      foreign_key: "widget_id"

If you like the formality of using full names for the asociations, you can turn off concise names globally or per-model, see Configuration.

Ordering has_many using position

If the target of a has_many association has a column named position, SchemaAssociations will specify order: :position for the association. That is,

create_table :comments do |t|
    t.integer post_id
    t.integer position

leads to

class Post < ActiveRecord::Base
    has_many :comments, order: :position

Table names, model class names, and modules

SchemaAssociations determines the model class name from the table name using the same convention (and helpers) that ActiveRecord uses. But sometimes you might be doing things differently. For example, in an engine you might have a prefix that goes in front of all table names, and the models might all be namespaced in a module.

To that end, SchemaAssociations lets you configure mappings from a table name prefix to a model class name prefix to use instead. For example, suppose your database had tables:


The default model class names would be


But if instead you wanted


you would define the mapping in the configuration:

SchemaPlus.setup do |config|
    config.table_prefix_map["hpy_"] = "Happy::"

Tables names that don't start with hpy_ will continue to use the default determination.

You can set up multiple mappings. E.g. if you're using several engines they can each set up the mapping for their own modules.

You can set up a mapping from or to the empty string, in order to unconditionally add or remove prefixes from all model class names.

How do I know what it did?

If you're curious (or dubious) about what associations SchemaAssociations defines, you can check the log file. For every assocation that SchemaAssociations defines, it generates a debug entry such as

[schema_associations] Post.has_many :comments, :class_name "Comment", :foreign_key "comment_id"

which shows the exact method definition call.

SchemaAssociations defines the associations lazily, only creating them when they're first needed. So you may need to search through the log file to find them all (and some may not be defined at all if they were never needed for the use cases that you logged).


You can configure options globally in an initializer such as config/initializers/schema_associations.rb, e.g.

SchemaAssociations.setup do |config|
  config.concise_names = false

and/or override the options per-model, e.g.:

class MyModel < ActiveRecord::Base
  schema_associations.config concise_names: false

Here's the full list of options, with their default values:

SchemaAssociations.setup do |config|

  # Enable/disable SchemaAssociations' automatic behavior
  config.auto_create = true
  # Whether to use concise naming (strip out common prefixes from class names)
  config.concise_names = true
  # List of association names to exclude from automatic creation.
  # Value is a single name, an array of names, or nil.
  config.except = nil
  # List of association names to include in automatic creation.
  # Value is a single name, and array of names, or nil.
  config.only = nil

  # List of association types to exclude from automatic creation.
  # Value is one or an array of :belongs_to, :has_many, :has_one, and/or
  # :has_and_belongs_to_many, or nil.
  config.except_type = nil

  # List of association types to include in automatic creation.
  # Value is one or an array of :belongs_to, :has_many, :has_one, and/or
  # :has_and_belongs_to_many, or nil.
  config.only_type = nil

  # Hash whose keys are possible matches at the start of table names, and
  # whose corresponding values are the prefix to use in front of class
  # names.
  config.table_prefix_map = {}


SchemaAssociations is tested on all combinations of:

  • ruby 2.3.1 with activerecord 4.2, using mysql2, postgresql or sqlite3
  • ruby 2.3.1 with activerecord 5.0, using mysql2, postgresql or sqlite3


  • As of version 1.2.3, rails < 4.1 and ruby < 2.1 are no longer supported
  • As of version 1.2.0, ruby 1.9.2 is no longer supported.
  • As of version 1.0.0, ruby 1.8.7 and rails < 3.2 are no longer supported.


Install from via

$ gem install "schema_associations"

or in a Gemfile

gem "schema_associations"


SchemaAssociations is tested against the matrix of combinations. To run the full combo of tests, after you've forked & cloned:

$ cd schema_associations
$ schema_dev bundle install
$ schema_dev rspec

For more info, see schema_dev

Code coverage results will be in coverage/index.html -- it should be at 100% coverage.

Release notes:


  • Support for AR5 (Rails 5).


  • Use schema_monkey rather than Railties.


  • Bug fix: Don't fail trying to do associations for abstract classes (mysql2 only). [#11, #12] Thanks to @dmeranda


  • Use schema_plus_foreign_keys rather than all of schema_plus, to eliminate unneeded dependancies. That limits us to AR >= 4.1 and ruby >= 2.1
  • Fix deprecations
  • Logging is now at debug level rather than info level


  • Bug fix (Rails workaround) for STI: propagate associations to subclasses, since Rails might not, depending on the load order.


  • Works with Rails 4.1
  • Test against MRI ruby 2.1.2


  • Works with Rails 4, thanks to @tovodeverett
  • Test against MRI ruby 2.0.0; no longer test against 1.9.2


  • New feature: config.table_prefix_map


  • Bug fix: use singular :inverse_of for :belongs_to of a :has_one


  • Use :inverse_of in generated associations

  • Drop support for ruby 1.8.7 and rails < 3.2


  • SchemaAssociations is derived from the "Red Hill On Rails" plugin foreign_key_associations originally created by harukizaemon (

  • SchemaAssociations was created in 2011 by Michal Lomnicki and Ronen Barzel


This gem is released under the MIT license.