# How to Load Data Into DB

**URL:** <https://www.ruby-forum.com/t/how-to-load-data-into-db/141215>\
**Category:** Rails\
**Created:** [June 23, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-load-data-into-db/141215 "2008-06-23T18:45:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 23, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-load-data-into-db/141215/1 "2008-06-23T18:45:10Z")

</div>

Is there a way of automatically initializing a database with sample  
data? I ask because fixtures seems to be geared towards the test  
database (different from the development version) and that too it loads  
one table at a time (maybe I could have calls to all the fixtures in one  
test file?). Is there a typical way the rest of you are initializing a  
database with data for usage?

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 23, 2008, 7:10pm UTC](https://www.ruby-forum.com/t/how-to-load-data-into-db/141215/2 "2008-06-23T19:10:09Z")

</div>

check out seed\_fu,  
[http://intridea.com/2008/4/20/seed-fu-simple-seed-data-for-rails](http://intridea.com/2008/4/20/seed-fu-simple-seed-data-for-rails)

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 23, 2008, 8:32pm UTC](https://www.ruby-forum.com/t/how-to-load-data-into-db/141215/3 "2008-06-23T20:32:46Z")

</div>

I’ve also been on projects where migrations are used to seed the data.

Best.  
Mike

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 23, 2008, 9:13pm UTC](https://www.ruby-forum.com/t/how-to-load-data-into-db/141215/4 "2008-06-23T21:13:58Z")

</div>

You can also create a controller with generator methods based upon your  
needs:

def test\_products  
quantity = params[:id].to\_i  
quantity.times do |i|  
product = Product.new(  
:created\_at =\> Date.today,  
:name =\> ‘Acme yoyo’  
:price =\> ‘5.00’)  
product.save  
end  
render :text =\> “Generated #{quantity} products.”  
end

You can edit this as needed, then execute this via url passing the # of  
products you want.

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 23, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/how-to-load-data-into-db/141215/5 "2008-06-23T21:41:09Z")

</div>

Michael B. wrote:

> I’ve also been on projects where migrations are used to seed the data.
> 
> Best.  
> Mike

I have encountered some where the migration has “MODEL\_NAME”.create  
:FIELD=\>VALUE  
… it sounds good … maybe I could try that … it seems the easiest to  
do . However, I like the flexibility of fixtures and the controller  
approach.

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 23, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/how-to-load-data-into-db/141215/6 "2008-06-23T22:19:38Z")

</div>

On Jun 23, 2008, at 3:12 PM, MW Administrator wrote:

> You can also create a controller with generator methods based upon  
> your  
> needs:

Just make sure you don’t deploy this controller to the production site.

Best.  
Mike
