Database Articles

  Home arrow Database Articles arrow Publisher Administration for Book Inve...

Publisher Administration for Book Inventory Management
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0

    Table of Contents:
  • Publisher Administration for Book Inventory Management
  • Generating Publisher Code with the Scaffolding Script



    Publisher Administration for Book Inventory Management

    (Page 1 of 2 )

    In this second part of a series on working with the scaffolding feature of Ruby on Rails, you'll lean how to implement a publisher administration interface for a book inventory management system. This article is excerpted from chapter three of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

    Implementing the Publisher Administration Interface

    We will start by implementing the administrator interface for maintaining the list of publishers. We need a table for storing publishers, so the first thing we need to do is to update the database schema by adding the publishers table to the database schema.

    Updating the Schema with the Publishers Table

    As in the previous chapter, we will use ActiveRecord migrations to make the necessary modifications to the database schema. We could also use plain SQL, but migrations have the added benefit of being database-agnostic and allowing you to roll back changes.

    First, create thecreate_publishersmigration file, which you will use for adding thepublisherstable to the database schema:

    $ script/generate migration create_publishers

    exists db/migrate
    create db/migrate/002_create_publishers.rb

    Opendb/migrate/002_create_publishers.rbin your editor and change it as follows:

    class CreatePublishers < ActiveRecord::Migration
    def self.up
    create_table :publishers do |table|
    table.column :name, :string, :limit => 255, :null => false, :unique => true

    def self.down
    drop_table :publishers

    The migration will create a table namedpublishers when run, as the following sample output shows:

    $ rake db:migrate

    --------------------------------------------(in /home/george/projects/emporium)
    == CreatePublishers: migrating ================================================ -- create_table(:publishers)
    -> 0.2030s
    == CreatePublishers: migrated (0.2030s) =======================================


    The new table has two columns:idandname. Note that theidcolumn is automatically added by ActiveRecord migrations, so we need to add only thenamecolumn to the migration script. We limit thename columnís length to a maximum length of 255 characters. We also specify that we donít accept null values in thenamefield and that the name must be unique.

    Following good practices, we undo all changes in thedownmethod by telling ActiveRecord to delete thepublisherstable.

    More Database Articles Articles
    More By Apress Publishing

    blog comments powered by Disqus


    - MariaDB, a MySQL Alternative, Opens Foundati...
    - Oracle Supporting HP Itanium Again, Finally
    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual

    Developer Shed Affiliates


    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap