Home arrow Ruby-on-Rails arrow Page 3 - Preparing to Add Tagging Support

Preparing to Add Tagging Support

In this second part of a four-part series on how to add tagging support to an ecommerce website using Ruby on Rails, you'll learn what modifications you need to make so that items in your system can be tagged without breaking things. This article is excerpted from chapter seven 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
June 09, 2010
  1. · Preparing to Add Tagging Support
  2. · Unit Testing the Model
  3. · Using the Console to Test the Model

print this article

Preparing to Add Tagging Support - Using the Console to Test the Model
(Page 3 of 3 )


You normally test an application with your browser or with unit, integration, and functional tests. The console script, located in your applicationís scriptdirectory, provides you with one more option. When executed, the script loads your application and opens an interactive session that you can use to write and execute code directly in the console window. You can create, update, and delete objects and access your controllers. This is useful when you want to perform additional testing.  

Tip  By default, theconsole script loads your development environment. But you can also use it on your production machine, for example, for doing maintenance work on your database. Simply executescript/console productionand write some code that uses your ActiveRecord models as you would in a controller.

Letís use the console to double-check that tagging works. Open a command prompt and start the Emporium console by executingscript/consolein the applicationís root directory. You can runscript/console --helpto get a list of options.

When the console has loaded, you should see the following:

$ script/console

Loading development environment.

The console is now waiting for input, and you can write the following code in the console to create a publisher, author, and a book:

publisher = Publisher.create(:name => 'A Publisher')
author = Author.create(:first_name => 'An', :last_name => 'Author')
book = Book.create(
  :title => 'A Book',
:publisher => publisher,
:authors => [author],
:published_at => Time.now,
:isbn => '123-123-123-x',
:blurb => 'The blurb',
:page_count => 300,
:price => 30.5 )

Press Enter to execute the code and print out the object to the console, as follows (this is a partial listing):

=> #<Book:0xb745b42c @new_record=false, @authors=[#<Author:0xb748d314 @new_record...

You can now use thebookobject that was created and tag it with the code shown here:

book.tag('A B C D E F')

Recall that thesave method should returntrue if the save was successful. You can verify that the book was tagged correctly by executing a search for books that have been tagged with either A or B:

Book.find_tagged_with(:any => 'A B')

=> [#<Book:0xb73f0820 @attributes={"isbn"=>"123-123-123-x", "updated_at"=>"2006...

You should see the method print out to the console the object that you created.

Please check back tomorrow for the third part of this series.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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