Home arrow Ruby-on-Rails arrow Adding Authors to an Online Bookstore
RUBY-ON-RAILS

Adding Authors to an Online Bookstore


In this second part of a four-part article series on creating an online bookstore application with Ruby on Rails, you'll learn how to run a migration script and unit tests, create the controller, and more. This article is excerpted from chapter two of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
May 12, 2010
TABLE OF CONTENTS:
  1. · Adding Authors to an Online Bookstore
  2. · Running Unit Tests
  3. · Creating the Controller
  4. · Implementing the User Stories

print this article
SEARCH DEVARTICLES

Adding Authors to an Online Bookstore
(Page 1 of 4 )

Running the First Migration

After we are finished editing the migration file, we can run the first migration, as follows:

$ rake db:migrate

--------------------------------------------
(in /home/george/projects/emporium)
== CreateAuthors: migrating =================================================== -- create_table(:authors)
  
-> 0.1776s
== CreateAuthors: migrated (0.1778s) ==========================================
--------------------------------------------

Rails uses the development environment by default, so our development database is now updated to match our migration file. If you want to do the migration to some database other than the development database, you can do so by specifying theRAILS_ENVenvironment variable before therakecommand:

$ RAILS_ENV=production rake db:migrate

So what really happened when our migration script was run? First of all, we got a new table calledauthors. But as this was the first migration, Rails also automatically created a table calledschema_info. The table has only one field,version, and there should be only one row in that table at any given moment. The value of the row tells the current migration version of the database schema. The migration scripts use this information to determine which migrations need to be run to get everything up-to-date.

Last, the migration created a file calleddb/schema.rb, which is in the same format as the migration files, and always describes the current state of the whole database schema. After the file was created, future migrations will automatically keep it up-to-date. Therefore, you should never edit it by hand.


RAKE

Rake (http://rake.rubyforge.org/) is a build language and tool similar to make (www.gnu.org/software/make/) and ant (http://ant.apache.org/). It is written in Ruby and sports its own DSL for
handling the maintenance of a Ruby application. Rails uses Rake extensively for many kinds of tasks. The following are some of the most popular Rake tasks used in Rails. For a complete list, run rake -T in the root of your Rails application directory.

  • rake: Running rake without any parameters will rebuild the test database according to the migrations, and run all unit, functional, and integration tests found in the test directory.
  • rake db:migrate: Updates the database of the current environment to the latest version. You can specify the target version by appending VERSION=x after the command.
  • rake db:sessions:create: Creates a table for storing user session data in the database. Rails
    automatically assigns a session cookie for each user and uses it to track the user. The session mechanism is very useful for tasks like user authentication, as youíll see in Chapter 8.
  • rake db:sessions:clear: Purges the sessions table. It is a good idea to schedule this command to run on regular intervals to keep the table size from growing rapidly. Every new visitor to the application will result in a new database row in the sessions table.
  • rake log:clear: Truncates the log files of your Rails applicationís log directory. Just like session
    data, the log files can get massive over time, so itís a good idea to clear them every once in a while.
  • rake rails:freeze:gems: Locks your Rails application to the latest version of Rails gems you have
    installed on your system. Without running this command (or rake rails:freeze:edge), your application will "float" on the latest gem version, which might lead to problems if there are changes in Rails code that break backward-compatibility.
  • rake rails:freeze:edge: Similar to rake rails:freeze:gems, with the distinction that it locks the Rails code to the latest (possibly unstable) code in the Rails Subversion source code repository. The Rails code is copied to the vendor/rails directory in your application tree.
  • rake rails:unfreeze: Breaks the connection between the application and Rails version that was created by either of the freeze tasks just described.
  • rake stats: Outputs useful statistics about your application, including lines of code and the code to test ratio.


Rake is a very powerful tool that you can use to automate many of the repetitive and tedious maintenance
tasks in your application. If you want to know more about Rake, Martin Fowler has written an excellent tutorial called "Using the Rake Build Language" (http://martinfowler.com/ articles/rake.html).



blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials