百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

populator

> 编程语言
开源

大量填充 Active Record 数据库。

394 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

大量填充 Active Record 数据库。

= Unmaintained

The Populator gem is no longer maintained. Feel free to fork this project.

= Populator

RDocs[http://rdoc.info/projects/ryanb/populator] | Screencast[http://railscasts.com/episodes/126-populating-a-database]

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}[http://faker.rubyforge.org].

== 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}[https://github.com/ryanb/populator/issues] or fork the project and send a pull request.

See {spec/README}[https://github.com/ryanb/populator/blob/master/spec/README.rdoc] 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[https://github.com/ryanb/populator/contributors]. See the CHANGELOG[https://github.com/ryanb/populator/blob/master/CHANGELOG.rdoc] for the full list.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言