Giter Site home page Giter Site logo

populator's Introduction

Populator

RDocs | Screencast

Populate an Active Record database with mass insertion.

Installation

Rails 3 support is currently being worked on. Stay tuned.

In Rails 2, install the gem.

gem install populator

And then load it in a rake task or elsewhere.

require "populator"

Usage

This gem adds a “populate” method to all Active Record models. Pass the number of records you want to create along with a block. In the block you can set the column values for each record.

Person.populate(3000) do |person|
  person.first_name = "John"
  person.last_name = "Smith"
end

This will do a mass insert into the database so it is very fast. The person object contains the “id” so you can set up associations.

Person.populate(3000) do |person|
  person.first_name = "John"
  person.last_name = "Smith"
  Project.populate(30) do |project|
    project.person_id = person.id
  end
end

That will create 30 projects for each person.

Passing a range or array of values will randomly select one.

Person.populate(1000..5000) do |person|
  person.gender = ['male', 'female']
  person.annual_income = 10000..200000
end

This will create 1000 to 5000 men or women with the annual income between 10,000 and 200,000.

You can pass a :per_query option to limit how many records are saved per query. This defaults to 1000.

Person.populate(2000, :per_query => 100)

If you need to generate fake data, there are a few methods to do this.

Populator.words(3) # generates 3 random words separated by spaces
Populator.words(10..20) # generates between 10 and 20 random words
Populator.sentences(5) # generates 5 sentences
Populator.paragraphs(3) # generates 3 paragraphs

For fancier data generation, try the Faker gem.

Important

For performance reasons, this gem does not use actual instances of the model. This means validations and callbacks are bypassed. It is up to you to ensure you’re adding valid data.

Development

Problems or questions? Add an issue on GitHub or fork the project and send a pull request.

See spec/README for instructions on running specs.

Special Thanks

Special thanks to Zach Dennis for his ar-extensions gem which some of this code is based on. Also many thanks to the contributors. See the CHANGELOG for the full list.

populator's People

Contributors

bryanstearns avatar ordinaryzelig avatar phlawski avatar ryanb avatar ttreyer avatar

Watchers

 avatar  avatar

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.