Wednesday, December 11, 2013

RoR Music Database Project

Finally, I've created a Rails + SQLite version of my favorite practice project - the Music Database. This one looks very different from my other (Perl, Python, PHP, ASP, ASPX) implementations of the project - as Ruby is an object-oriented language, I tried to model the application as closely as possible to real life. Usually I store the details of each song in a row of the database table, with the album details (Album Name, Year, Artist Name) being repeated for multiple songs. But here I made Album the primary model and Song a secondary one. So the user has to first create an album and then add multiple songs to it. Here are the steps I followed, once I had the design ready in my mind:

  rails new MusicDatabase
  cd MusicDatabase
  rails generate scaffold Album albumname:string albumartist:string albumyear:datetime
  rake db:migrate

Edit the file "config\routes.rb" to open "views\albums\index" by default, by adding the line:
  root :to => "albums#index"

rails generate model Song songname:string album:references
rake db:migrate
rails generate controller Songs index show new edit

Edit the file "controllers\song_controller.rb" to have the standard methods for showing, adding, editing and deleting songs

Edit the files under the "views\songs\" directory to have the standard forms for showing, adding, editing and deleting songs

In my case, because of the form code that I copied from the Rails tutorial, I also had to edit my Gemfile to include the line "gem 'dynamic_form'" and then do a "bundle install". And thus, I completed my Ruby on Rails "self test" by getting the Music Database project running successfully

No comments:

Post a Comment