0.0
No commit activity in last 3 years
No release in over 3 years
If you've gone through the trouble of linking your schema with proper foreign keys, defining associations in ActiveRecord feels like double work. ActiveSchema discovers the associations and validations that can be derived from the database schema.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.0
~> 1.5.0.pre3
>= 0
>= 0
>= 2.0.0.beta.22
>= 0
>= 2.0.0.beta.22

Runtime

 Project Readme

ActiveSchema

ActiveSchema makes ActiveRecord a bit DRYer. It discovers associations, such as belongs_to and has_many, using foreign keys, and it adds validations to ensure constraints on data, like NOT NULL and maximum length, are honored.

An example

If you have a table structure like this (arrows indicate foreign keys)

ActiveSchema would link your models like below

class Prisoner
  belongs_to :facility
end

class Facility
  has_many :facilities
  belongs_to :warden
end

class Warde
  has_one :facility
end

Usage

Put

gem 'activeschema'

in your Gemfile.

ActiveSchema can be enabled per model, or you can choose to make it available everywhere.

Either way, it must be activated by the active_schema class method. Per model:

class Model < ActiveRecord::Base
  active_schema
end

In ActiveRecord::Base:

class ActiveRecord::Base
  active_schema
end

Foreign key support by ‘foreigner’

Foreign key information is extracted by the Foreigner library.
It has out-of-the-box support for MySQL, Postgresql, and SQL2003.

Rails 3 and Ruby 1.9.2

ActiveSchema has only been tested on Rails 3 and Ruby 1.9.2. It may work elsewhere, but there really is no guarantee.

Preloading the associations

The speed at which MySQL supplies foreign key information can, at times, be leisurely, to say the least.

To circumvent this, ActiveSchema supports foreign key extraction without hitting the database.
Instead, it reads the information from the dumped “schema.rb” file, which of course must be current.

Adjusted configuration:

schema_feeder = ActiveSchema::SchemaFeeder.new
schema_feeder.read("path/to/schema.rb")
ActiveSchema.configure do |c|
   c.feeder = schema_feeder
end