Home arrow Ruby-on-Rails arrow 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
(Page 1 of 3 )

Preparing the Models

In Chapter 3, we created the Book model. Before creating the Tagmodel, we need to modify theBookmodel so that it can be tagged. This is a simple operation. Just add theacts_as_taggablemethod call to the model, as shown here:  

class Book < ActiveRecord::Base

This gives us access to theacts_as_taggableAPI methods listed in Tables 7-1 and 7-2. We can now do things likeBook.find_by_title('The Satanic Verses').tag('Novel, Blasphemous')andBook.find_tagged_with(:any => 'Blasphemous')with the model. As you can see, the code reads almost like a sentence written in English.

Next, create the ActiveRecord model for thetagstable by executing thegeneratescript:

$ script/generate model Tag --skip-migration

  exists  app/models/
  exists  test/unit/
  exists  test/fixtures/
  create  app/models/tag.rb
  create  test/unit/tag_test.rb
  create  test/fixtures/tags.yml

We tell thegeneratescript not to generate a migration file, since we already created it manually. You can open theapp/models/tag.rband examine it. You should see the following code:

class Tag < ActiveRecord::Base

TheTag model doesnít include a mapping to theBook model. This means you canít access the books that are associated with a tag by calling, for example,Tag.find(1).books. This wonít be a problem, as we will use theacts_as_taggableAPI instead, which does the same work with theBook.find_tagged_withmethod. However, if you need it, you can add the mapping by addinghas_and_belongs_to_many :booksto theTagmodel.

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