dumpable

Output all the sql commands necessary to generate a database row and its relationships.

Usage

class User < ActiveRecord::Base
  has_many :posts
  has_many :roles

  dumpable [:roles, {:posts => :comments}]
end

class Role < ActiveRecord::Base
  belongs_to :user
end

class Post < ActiveRecord::Base
  has_many :comments
end

class Comments < ActiveRecord::Base
  belongs_to :post
end

Then, from the console, you can do something like:

User.first.dump

This will spit out all the MySQL insertion code for the user, it’s roles, it’s posts, and each post’s comments

If you want all to pad the ids generated in the insertion code, this can be done as follows:

User.first.dump(:id_padding => 1000)

This will ensure that the id of the above user is padded by 1000, so a user id of 1 will generate an insert statement with an id of 1001.

You may wonder why the id is included as part of the insert statement. This is to ensure that all associations maintain their relations to one another. For example, if TODO

All ActiveRecord models are dumpable automatically, so you could also call:

Post.dump

You can also dump all the entries in the table by calling:

Post.dump
# or
Post.where(:published => true).dump

Gotchas

At the moment, Dumpable will not work on any complex relationships. This includes has_many :through, and has_and_belongs_to_many. It also will not work on models that have a different primary key than id or on models with complex keys.

It is also only setup to work with MySQL databases and is untested with any other databases.

Copyright © 2013 Andrew Hunter. See LICENSE.txt for further details.