# Pagination vs. Tags for viewing featured items

**URL:** <https://www.ruby-forum.com/t/pagination-vs-tags-for-viewing-featured-items/86262>\
**Category:** Rails\
**Created:** [February 21, 2007, 12:21am UTC](https://www.ruby-forum.com/t/pagination-vs-tags-for-viewing-featured-items/86262 "2007-02-21T00:21:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tag\_Sadness](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Tag\_Sadness](https://www.ruby-forum.com/u/Tag_Sadness)\
**Post date:** [February 21, 2007, 12:21am UTC](https://www.ruby-forum.com/t/pagination-vs-tags-for-viewing-featured-items/86262/1 "2007-02-21T00:21:25Z")

</div>

Goal:

- Label certain “items” in the database as “featured.”
- View the “featured items” on a list page
- On the list page, each “featured item” links to a item show page.
- On each “featured item” show page, there are links to show the  
previous or next “featured item”
- The show “previous featured item” and show “next featured item”  
links should be automagicly generated in the layout.

There are probably a number of ways to accomplish this, but my big  
n00b is getting in the way.

First Guess: Pagination Module  
My first guess was that I could use something within the Pagination  
module, but so far Pagination appears to be limited to taking all the  
“items” in the database, and showing them in order by id in sets of a  
X number of items.

I don’t see a pagination option for labeling certain items as  
“featured” and then listing only those items, nor a way of placing  
links in the view so as to store the position within the “featured  
item list” as the browser moves from “featured item” page to “featured  
item” page.

Second Guess: Acts\_as\_Taggable Plugin  
Second, I tried using the act\_as\_taggable plugin (not gem) and then  
the act\_as\_taggable\_on\_steroids plugin. This pretty much fell apart  
from the start since I spent most of the day hunting down missing  
setup instructions.

The wiki HowTo says one thing, the on\_steroids site says another.  
Neither seems to include all the information necessary to set the  
plugin up. I tried following the “Rails Recipes” book (The Pragmatic  
Programmer has a free PDF download of Chapter 19) but it is  
embellished with trips to the “ruby script/console”, layered partials,  
and ajax – all of which really confused me.

If I go this route, it seems like I need a join table, but not a  
table1\_table2 join table, but instead a special join table named  
“taggings”.

Anyway, I am starting to whine more than ask for help, so … help …  
anyone?

My best guess at this point is:

1. Create a tags table with an “id” field as primary key and “name”  
field
2. Create a tag named “featured”
3. ?? Use a join table between tags\_items to tag specific “items” as  
“featured”
4. ?? Controller: Some how assemble an array of only the “featured  
items”
5. ?? View: Index page renders each featured\_item in @featured\_items
6. ?? View: Item page somehow set the previous/next links to  
current\_array\_item ±1
7. Weep for joy that someone helped me get it to work.

---

<div class="post-metadata">

**Author:** ![Tag\_Sadness](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Tag\_Sadness](https://www.ruby-forum.com/u/Tag_Sadness)\
**Post date:** [February 21, 2007, 2:07am UTC](https://www.ruby-forum.com/t/pagination-vs-tags-for-viewing-featured-items/86262/2 "2007-02-21T02:07:32Z")

</div>

If I got it right, you can use pagination (pagination helper is not  
necessary)

Controller  
@featured = Item.find(:all, :conditions =\> “type=‘featured’”, :limit =\>  
1,  
:offset =\> #{page})  
@total\_featured = Item.count(“id”, “type=‘featured’”)

View  
[Previous](https://www.ruby-forum.com/items/featured/?page=<%=%20page-1%20%>)  
[Next](https://www.ruby-forum.com/items/featured/?page=<%=%20page+1%20%>)

You need some checks to for the previous/next links.

G.

---

<div class="post-metadata">

**Author:** ![Tag\_Sadness](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Tag\_Sadness](https://www.ruby-forum.com/u/Tag_Sadness)\
**Post date:** [March 1, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/pagination-vs-tags-for-viewing-featured-items/86262/3 "2007-03-01T20:36:07Z")

</div>

Thanks for the suggestion Bandito. Your code got me started thinking  
in the right direction. I’m not sure I totally grasped what you  
proposed, but it did help me get the first chunk of my goal done.

I walked away from the act\_as\_taggable / act\_as\_taggable\_on\_steroids  
plugins. I don’t need a full tag system, so why implement one.

```
On Feb 20, 3:20 pm, "Tag_Sadness" <removed_email_address@domain.invalid> wrote:
    > Goal:
    > - Label certain "items" in the database as "featured."
    > - View the "featured items" on a list page
    > - On the list page, each "featured item" links to a item

```

show page.

Instead I add a column named “featured” to my “items” databasea nd  
assigned a few items to have a “featured” value of 1 (e.g. 1 = yes, 0  
= no).

```
ALTER TABLE items ADD featured int(1) NOT NULL

UPDATE items SET featured = 1 WHERE id = 5

```

Then in my controller:

```
def list
    @items = Item.find(:all)

    featured_items = []

    for item in @items
        if item.featured == 1
            featured_items << item
        end
    end

    @features = featured_items
end

```

Then in my “list” view:

```
<div class="item-thumbnails">
    <% for feature in @features -%>
        <% for image in feature.images -%>
            <% if image.position == 5 -%>
                <%= link_to( image_tag( image.file_name, :title =>

```

feature.title, :alt =\> image.alt ),  
{:action =\>  
‘show’, :id =\> feature.id} ) -%\>  
\<% end-%\>  
\<% end-%\>  
\<% end-%\>

Obviously I had a pre-existing relationship between items and images  
that persists when an item is inside the @features array. There is  
probably a way to move some of this code back to the controller, but I  
haven’t gotten anything to work quite yet. I wish I had a better  
programming foundation. It will come in time with practice I guess?

So now with that working, my next set of goals are:

```
On Feb 20, 3:20 pm, "Tag_Sadness" <removed_email_address@domain.invalid> wrote:
    > - On each "featured item" show page,
        there are links to show the previous or next "featured

```

item"

```
    > - The show "previous featured item" and show "next featured

```

item"  
links should be automagicly generated in the layout.

This is where I think Bandito has filled in some blanks. I didn’t have  
the @features array populated with data before.

Now I _think_ I need to use the “limit and offset” to move through the  
@features array one at a time using the simple math in the anchor  
element bandito provided.

Wish me luck. More advise is always appreciated. And again, thank you  
Bandito.
