Database Articles

  Home arrow Database Articles arrow Implementing Models for a Book Invento...

Implementing Models for a Book Inventory Management System
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0

    Table of Contents:
  • Implementing Models for a Book Inventory Management System
  • Adding Validations to the Book Model



    Implementing Models for a Book Inventory Management System

    (Page 1 of 2 )

    In this fifth part of a multi-part series on the scaffolding feature of Ruby on Rails, we'll focus on imiplementing the many models we've been creating for our book inventory management system. This article is excerpted from chapter three of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

    Modifying the Generated Models

    Now that you understand both the database schema and the way ActiveRecord maps to the schema, let’s modify the generated models.

    Adding the has_many Mapping to the Publisher Model

    As we just explained, the one-to-many relationship between thePublisherandBookmodel is set up in ActiveRecord by adding thehas_manydeclaration toapp/models/publisher.rb, as highlighted in the following code snippet:

    class Publisher < ActiveRecord::Base
    has_many :books

    validates_length_of :name, :in => 2..255
    validates_uniqueness_of :name

    This gives you access to, for example, thebooks.empty?method:


    In case you are wondering,find_by_nameis a dynamic finder, which dynamically (at run-time) creates a SQL query that returns the Apress publisher, or nil if the publisher is not found.


    Dynamic finders are features of ActiveRecord that allow you to use the ActiveRecord API instead of SQL to find
    objects. Dynamic finders use the find_by format and are created by ActiveRecord on the fly at runtime when
    calling the following, for example:


    As another example, you could make this call:

    book.find_all_by_title_and_page_count(‘Drinking Tequila for Dummies’, 538)

    This dynamically creates a SQL query that finds all books having both the specified title and page count.
    Dynamic finders can also create a new record if the query returns no results. This is useful for implementing
    one-liners like this:


    This example creates the publisher Apress (if it doesn’t already exist) and then returns it.

    Adding the belongs_to Mapping to the Book Model

    As you learned in the previous section about ActiveRecord mappings, the many-to-one relationship between theBookandPublishermodel is set up with ActiveRecord by usingbelongs_to. Changeapp/models/book.rbas shown here:

    class Book < ActiveRecord::Base
    belongs_to :publisher

    Thebelongs_toallows you to access, for example, the name of the publisher from theBookmodel:

    Book.find_by_title(‘Elvis Peanut Butter Sandwich Recipes 5th Edition’)

    Adding the habtm Mapping to the Book and Author Models

    Next, for the many-to-many mapping between the authors and books, add thehas_and_belongs_to_manymapping toapp/models/book.rbas follows:

    class Book < ActiveRecord::Base
    has_and_belongs_to_many :authors
    belongs_to :publisher

    Note ActiveRecord tries to guess the name of the join table,authors_books, by combining the two table names. In our example, ActiveRecord will look for a table namedauthors_books, notbooks_authors, since the stringauthorscomes beforebookswhen compared in lexical order.

    We also want to be able to access the books from the author’s side of the relationship, so changeapp/models/author.rbas follows:

    class Author < ActiveRecord::Base
    has_and_belongs_to_many :books

    validates_presence_of :first_name, :last_name

    def name
    "#{first_name} #{last_name}"

    That takes care of the ActiveRecord mappings, but we also want to make sure only valid books are stored in the database. This can be done with validations, which we introduced in Chapter 2.

    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