Seeding rails application with data

While development I like to have some sample data for testing and demo purposes. Instead filling them out using application interface, rails console or database operation rails has a slick way of seeding your database.

When you create a new rails application you can find a file called seeds.rb on db directory. This file can contain all your seeding data as follows

projects = Project.create([{:title => 'xxxxx', :description => 'yyyyyyyy'}, {:title => 'rrrrrrrrr', :description => 'zzzzzzzzzzzzz'}])

To execute this file and fill your database with seeding data run

rake db:seed

Note that if you run it more than once you’ll have your data filled twice in the database, to reset your database run

rake db:reset

Having your seeding data in ruby file can give you some advantages as you can use loops to fill data series or use other tools such as Faker. To use faker with your seed data:

1- Add gem ‘faker’ to your Gemfile

2- Add require ‘faker’ on top of seeds.rb

3- Use faker in your seed data

projects = Project.create([{:title => 'xxxxx', :description => Faker::Lorem.paragraph(10)}])

Install rmagick gem on windows 7

My MacBook Pro was broken few days ago and i had to deal with the pain of rails programming on windows 7 :(

one of the biggest pain I stumbled upon was getting rmagic gem to work on the windows machine. It took me about 8 hours to figure out how it is done.

  1. Install ruby DevKit http://github.com/downloads/oneclick/rubyinstaller/DevKit-tdm-32-4.5.2-20110712-1620-sfx.exe
  2. Install ImageMagick 6.6.x with windows installer with headers http://www.imagemagick.org/download/binaries/ImageMagick-6.7.3-3-Q16-windows-dll.exe (You should change the installation folder to c:\ImageMagic otherwise it won’t work)
  3. Set the following Environment variables
    set PATH = c:\ImageMagic;%PATH%
    set CPATH = c:\ImageMagic;%CPATH%
    set LIBRARY_PATH=c:\ImageMagic\lib;%LIBRARY_PATH%
  4. gem install rmagick
    This will install the latest rmagick (in this case 2.13.1)