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)

Happy singleton

I love Ruby (Well…., only the programming language, I actually hate the stone)

I don’t know what makes this programming language special to me, but I would like to share a small ahh moment i had while reading about ruby

We are all familiar with the standard GOF implementation for singleton

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Logger
  def initialize
    @log = File.open("log.txt", "a")
  end
 
  @@instance = Logger.new
 
  def self.instance
    return @@instance
  end
 
  def log(msg)
    @log.puts(msg)
  end
 
  private_class_method :new
end

That’s is the plain old singleton but with some Ruby magic it can be like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Logger
  def initialize
    @log = File.open("log.txt", "a")
  end
 
  @@instance = Logger.new
 
  def self.instance
    return @@instance
  end
 
  def log(msg)
    @log.puts(msg)
  end
 
  private_class_method :new
end

It is some of these small things that brings me closer to Ruby every day

Pragmatic approach to learn Ruby on Rails

Three years ago out of bore and frustration with .Net framework while cruising on the web I stumbled upon Ruby on Rails, and from the first moment it clicked. I started to learn the framework and felt in love with it. I was working on and off with it for the last 3 years mostly due to the lake of Rails project (most of people still attached to other famous technologies)

Here I want to share the learning approach that will get you up to speed with RoR with minimal frustration.

To be able to explain my approach I will have to explain what is RoR briefly, Ruby on Rails is a web development framework built using Ruby language.
First step here is to learn the language

Learning Ruby

  1. Setup your system: RoR work best on Linux/Unix/Mac os (but it also work fine on windows platform). if you want to have the best experience without investing much money you can install Linux as a virtual machine on your system or clear 20 GB on your hard drive and install it directly on your computer (My favorit approach). My recommended Linux distribution is Ubuntu
  2. If you installed linux and needs to be familiar with it you can use this pdf to introduce you to the mysteries world of linux
  3. If you installed Linux you can google the installation approach (will introduce it in a later post). and if you will use your windows system you need to install it using ruby installer at this time the recommended version is 1.9.2 p290. Mac machines comes already with Ruby interpreter installed.
  4. After setting up the environment the following resources have proved really helpful.
    why’s (poignant) Guide to Ruby
    Learning Ruby (O’Reilly)

Learning Rails

  1. First step is to write a very basic rails application and see how it works, the following link is my first choice to see RoR in action.
    http://guides.rubyonrails.org/getting_started.html
  2. I would recommend building a simple application (such as a blog, Task management…) without worrying so much about writing tests just to get yourself familiar with Rails environment.
  3. Authentication is a must in almost all project you can use the instructions in the following screencast to add authentication aspect to your application
    http://railscasts.com/episodes/209-introducing-devise or in text format
    http://www.asciicasts.com/episodes/209-introducing-devise
  4. Now you have an overview how RoR works and created a simple demo application. Make sure you go through most of the materials on Ruby Guides and familiarize yourself with Testing Rails Applications guide.
  5. Pick a real project and fire your command line and start programming
  6. Start using rSpec & Cucumber into your projects to create better tests, the best resource i found in this topic is The RSpec Book from Pragmatic Programmers.

Tools

You can using only the command line a text editor to build awesome rails apps (In fact I found it is the best way)

  • For Windows you can use e-texteditor
  • For Mac TextMate is your best friend
  • VI is my editor of choice but if you are new to linux Redcar can provide a good graphical interface editor

If you are the IDE type of person Aptana is a great IDE with wonderful support for Rails

Additional Resources

Happy Coding