Posts Tagged ‘ Ruby on Rails

Getting Started With Rails Migrations Part 1: Creating Migrations

A migration is simply a Ruby source file in your application’s db/migrate directory. These files help to make small changes to the database extremely easy and quick. Want to add a table or rename a column? Write another migration and run rake db:migrate. With automatic versioning you are able to see the changes made to the model just by looking at the latest migration file. No more repetitive SQL queries. Now you can access your data in an understandable way right in your code. With that, let’s get coding!

First we can use either the rails generator to create a migration or use the model generator, which will also give us a migration file. Note: you could create these files by hand, but it is less error-prone and probably more efficient to use a built-in generator.

To create a migration with the model generator we would do something like this:

ruby script/generate model post
  exists app/models/
  exists test/unit/
  exists test/fixtures/
  create app/models/post.rb
  create test/unit/post_test.rb
  create test/fixtures/posts.yml
  create db/migrate
  create db/migrate/20090113055719_create_posts.rb

To create a migration on its own we would do something similar to this:

ruby script/generate migration add_category_column
exists db/migrate
create db/migrate/20090113055952_add_category_column.rb

To run the migration we use the db:migrate task

rake db:migrate

When running the db:migrate Rake task, a schema_migrations table is looked for to keep track of migration versions. If one isn’t found it will be created on the fly. After each migration is run a row in the schema_migration table is added.

You can also use the db:migrate Rake task to force the database to a specific version by supplying it with the Version= parameter.

rake db:migrate VERSION=20090113055719

To revert the database to it’s original state supply Version= paramter with 0.

rake db:migrate VERSION=0

If you run a migration whose version is higher than the version of the current database, the migration will be applied. Somewhat differently, if you run a migration whose version is lower than the version of the current database, Rails will look for the migration file whose number matches the database version and undo it.

I hope this tutorial has helped you to begin to understand ActiveRecord and how useful migrations can be. In the next article in this series we look at what makes up a migration in more detail.

Getting Started With Ruby on Rails: Resources (Paid and Free)

I have gone through all of my Ruby on Rails resources and have come up with a list of what I think are the best resources to get you started with your Ruby on Rails journey. This will be the first of a few lists about Ruby on Rails I will be posting over the next week or two. Following the getting started list will be a list on intermediate resources, advanced resources, and finally a list of some commonly plugins and blogs that I really think will help boost your understanding of Ruby on Rails. So with that, let’s get started!

Free Resources

  1. First you’ll probably want to get Ruby on Rails installed on your machine. To do this is quite simple and this website will help you along the way. If you are on a relatively new mac, you probably already have Rails installed and therefore just running the command ‘gem install rails’ from the terminal will get you the newest version of rails and all of it’s dependencies. More on this on the official Ruby on Rails website.
  2. Rolling With Ruby on Rails Part 1 and Part 2 – This is a slightly outdated tutorial as it was written in 2005, but it still serves as a great introduction to the language of ruby and the framework rails provides. 
  3. Really Getting Started with Rails – Amy Hoy over at Slash7 has put together a great guide that not only covers the the basics of RoR, but also introduces the reader to the basic concepts of the Ruby scripting language.
  4. Getting Started With Ruby on Rails – The excellent A List Apart wrote an article not very long ago that shows the benefits of using RoR for a web project. It introduces the reader to the basics of ruby and many concepts of the rails MVC framework.
  5. Using Ruby on Rails for Web Development on Mac OSX – Apple has written an article on getting started using ruby on rails with a fresh install of Mac OS X. It is a great tutorial that takes you through the creation of a basic RoR web application.
  6. Ruby on Rails from Scratch – Probably my favorite of all the free tutorials I have listed. This is a four part series of very visual tutorials for learning RoR. I know most people are visual learners, so this series is great for you. In fact, parts of the series are video tutorials. Definitely worth checking out.
  7. Rails Framework Documentation – This is the Rails API. Get familiar with it because it will become extremely useful the more you learn to love it.

Read more

Happy New Year!

So apparently this year was a second longer than last year because there are just over 365 days in a year. I wanted to take this time to just wish everybody that follows me on twitter a happy new year and to expect much more frequent posts from me this year(as it is one of my new year resolutions). I will be writing some articles about ruby on rails and my recent experiences soon. I hopefully will also be launching a web application my friend and I have been writing sometime this spring. More details to follow shortly on that.

As far as everything else goes, I’m headed back to Pittsburgh in a few days for my spring semester where I will be focusing pretty much full time on computer science now.

Anyways, I will be updating to wordpress 2.7 later today(when I wake up) and hopefully that will go smoothly.

Happy New Year!

RJS Demystified WIth Pretty Colors! Also 5 RoR Tips for Beginners…

Amy Hoy over at Slash7 has put together an amazing cheatsheet for RJS (ruby javascript) documents. It is 3 pages long and uses lots and lots of bright colors to illustrate clearly how rjs is interpretated.

She asks viewers to resist the urge of putting it on Digg yet, as her server is not ready to take another “Digging” quite yet.

View Amy’s post here

View the cheatsheet directly here

I would also like to mention a blog post I found today on Rails Forum which contains five tips to help beginners really get started with Ruby on Rails. Check it out here.

That’s it for now, but be sure to check back soon as there should be a tutorial on getting started with Adobe AIR up soon. I will then move onto more advanced topics and show you how to develop a basic twitter AIR app over the next few weeks. (or when mid-terms permit me to)

Ruby On Rails: Meaningful (and beautiful) URLs

This tutorial is going to take you through what it takes to make your URLs easy to remember and meaningful. Great examples of sites that implement this well are Flickr and Delicious. We are going to first take a look at the Routes framework in the Action Controller and see what options it gives us. I am going to assume basic knowledge of Ruby on Rails and am going to assume that your are starting with a newly generated application. (NOTE: I am using Rails 2.1.1)

Routes in Ruby on Rails allow you to map incoming URLs to specific controllers, actions, and parameters in your application. Routes are written in pure ruby and can be found in the file config/routes.rb in your application’s root directory. Upon generating a new application using rails appname, some default routes are created for you as shown below (comments have been removed):

1
2
3
4
ActionController::Routing::Routes.draw do |map|
    map.connect ':controller/:action/:id'
    map.connect ':controller/:action/:id.:format'
end

Read more