Git Product home page Git Product logo

models-to-sql-rails's Introduction

Introduction

Models to SQL Rails is a gem that allows you to dump ActiveRecord models graphs back into SQL or Rails fixtures format. With this gem you can easily convert a model or an array of models to a script like:

INSERT INTO modelName ('title','description') values ('Awesome Title', 'Wow, amaze description, much doge.');

This was just a example, this is a powerful and simple gem that can solve a lot of problems.

Usage

If you want use this gem in your project, you must add the following line to your gemfile:

# If you want the most recent version:
gem 'models-to-sql-rails', :git => 'git://github.com/paladini/models-to-sql-rails.git'

# If you want the most stable version:
gem "models-to-sql-rails", "~> 1.0.4"

Then, you must open your terminal (Ctrl+Alt+T) and run the following command on your project root path:

bundle install

Now you're ready to start coding - easy, no?

Examples

Imagine that you have a scenario like that:

require 'models_to_sql'

items = []
for i in 0..10
  items << Item.new(
      :name => generate_random_name(), 
      :description => generate_random_description(),
      :url => generate_random_url(),
      :type => "Goods",
      :category => "Electronics",
      :image_url => generate_random_image_url()
    )
end

###################################
#                                 #
#   Now let's play with our gem   #
#                                 #
###################################

items.to_sql_insert
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UKlRBjdsyzLtjCL', 'GWpDhkVtkUHkazW', 'CGeCMJjTQGPFUbc', 'Goods', 'Electronics', 'kQWPeRytZAedVnF');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UbOwdYzzFAKckNF', 'nlrMddCWRkYznBH', 'HOdGfDQBHmxSvSW', 'Goods', 'Electronics', 'PtWCKyhxrMFZVJd');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('NvQEUPVgKrPPaQh', 'uTPhqUvVTClimXA', 'HcTtNNoDjncnAIN', 'Goods', 'Electronics', 'LvtpaxTlLWblyar');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('GXSujkenwzvUqcS', 'QRjnvJROfZVSVEj', 'JoGNZMrjmUFlqVM', 'Goods', 'Electronics', 'yVJxiNEWFhQJbKv');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('gtJRWGAFOAvPzMa', 'TOFcwXlFxlLroTo', 'IlNmsRvShgbYace', 'Goods', 'Electronics', 'yLIBqYQjVIBeRcB');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('dUMiXZOzASvMyAv', 'nNuTUCjRsxNNoUU', 'qumOEoEpwGrJcjA', 'Goods', 'Electronics', 'PwbjaGayRZdwlKv');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('heuqrYERTtdUfgS', 'ZYckUtadljKbTBA', 'iFwqJYbqYYgEJNv', 'Goods', 'Electronics', 'BHHOXSFORpDsZBU');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('jFjywdyxTWrWrvC', 'MaJfdgHSENuHkrW', 'hnRJJwsSKHYCnvo', 'Goods', 'Electronics', 'fkKuTpgmvCYEzOK');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('JOIlNUFUVcMtQrP', 'PjLMkYvyFYGlRlr', 'MHucPxQxylqLQia', 'Goods', 'Electronics', 'quWbZUHSXwLvClY');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UxtBOSKroxWtShX', 'ORVtVgfCTonQIZH', 'SjWgwuZBxLLHKCe', 'Goods', 'Electronics', 'drOiSxLZAkFtbCW');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('juAiBiHnFybnjlL', 'ouDFQXnSkRIMQaR', 'hgifUygkuXAWPLp', 'Goods', 'Electronics', 'dKVCzaOCJwmxIim');

items[0].to_sql_insert
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UKlRBjdsyzLtjCL', 'GWpDhkVtkUHkazW', 'CGeCMJjTQGPFUbc', 'Goods', 'Electronics', 'kQWPeRytZAedVnF');

items.each do |item|
   item.to_sql_insert
end
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UKlRBjdsyzLtjCL', 'GWpDhkVtkUHkazW', 'CGeCMJjTQGPFUbc', 'Goods', 'Electronics', 'kQWPeRytZAedVnF');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UbOwdYzzFAKckNF', 'nlrMddCWRkYznBH', 'HOdGfDQBHmxSvSW', 'Goods', 'Electronics', 'PtWCKyhxrMFZVJd');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('NvQEUPVgKrPPaQh', 'uTPhqUvVTClimXA', 'HcTtNNoDjncnAIN', 'Goods', 'Electronics', 'LvtpaxTlLWblyar');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('GXSujkenwzvUqcS', 'QRjnvJROfZVSVEj', 'JoGNZMrjmUFlqVM', 'Goods', 'Electronics', 'yVJxiNEWFhQJbKv');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('gtJRWGAFOAvPzMa', 'TOFcwXlFxlLroTo', 'IlNmsRvShgbYace', 'Goods', 'Electronics', 'yLIBqYQjVIBeRcB');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('dUMiXZOzASvMyAv', 'nNuTUCjRsxNNoUU', 'qumOEoEpwGrJcjA', 'Goods', 'Electronics', 'PwbjaGayRZdwlKv');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('heuqrYERTtdUfgS', 'ZYckUtadljKbTBA', 'iFwqJYbqYYgEJNv', 'Goods', 'Electronics', 'BHHOXSFORpDsZBU');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('jFjywdyxTWrWrvC', 'MaJfdgHSENuHkrW', 'hnRJJwsSKHYCnvo', 'Goods', 'Electronics', 'fkKuTpgmvCYEzOK');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('JOIlNUFUVcMtQrP', 'PjLMkYvyFYGlRlr', 'MHucPxQxylqLQia', 'Goods', 'Electronics', 'quWbZUHSXwLvClY');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UxtBOSKroxWtShX', 'ORVtVgfCTonQIZH', 'SjWgwuZBxLLHKCe', 'Goods', 'Electronics', 'drOiSxLZAkFtbCW');
# => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('juAiBiHnFybnjlL', 'ouDFQXnSkRIMQaR', 'hgifUygkuXAWPLp', 'Goods', 'Electronics', 'dKVCzaOCJwmxIim');

Simple? If have any issue or problem, talk to us now.

Original documentation

Original README from this outdated gem. Have some useful informations that we don't have time to describe here.

After installation, each AR model has to_sql method that can take following options:

:ignore_associations_for - do not dump associations with specified models. (default: empty) :ignore_models - do not dump specified models. Array of ruby Class objects is used. (default: empty) :ignore_tables - do not dump specified tables (default: empty) :debug - debugging mode (default: false)

Contribute

If you do like to contribute to our project, please feel free to Fork our project or talk with us by creating a new issue. Rails 3.2 port by Martin Provencher and Rails 4.0 port + documentation by Fernando Paladini.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.