# What's the best way to solve this?

**URL:** <https://www.ruby-forum.com/t/whats-the-best-way-to-solve-this/193336>\
**Category:** Rails\
**Created:** [August 16, 2010, 7:05am UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-solve-this/193336 "2010-08-16T07:05:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 16, 2010, 7:05am UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-solve-this/193336/1 "2010-08-16T07:05:05Z")

</div>

Hi,

Here’s a situation:  
A product belongs to multiple categories and the categories are top  
menus of a site.  
I want to highlight the category of a product when the product detail  
page is loaded.  
If there’s some context, I can determine which category should be  
highlighted.  
(For example, I can track the categories that a user selected).  
But sometimes there’s no context (for example, the user visits the page  
directly without getting through categories).  
How can you solve this problem?

Thanks.

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 16, 2010, 10:17am UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-solve-this/193336/2 "2010-08-16T10:17:44Z")

</div>

On 16 August 2010 06:05, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> Here’s a situation:  
> A product belongs to multiple categories and the categories are top  
> menus of a site.

That does not make sense, do you mean product has\_many categories, or  
more likely category has\_many products and product belongs\_to  
category.

> I want to highlight the category of a product when the product detail  
> page is loaded.  
> If there’s some context, I can determine which category should be  
> highlighted.  
> (For example, I can track the categories that a user selected).  
> But sometimes there’s no context (for example, the user visits the page  
> directly without getting through categories).

If you have a product, @product say, then the the category of the  
product is @product.category, assuming you have setup the ActiveRecord  
relationships correctly.

Colin

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 16, 2010, 6:10pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-solve-this/193336/3 "2010-08-16T18:10:20Z")

</div>

Colin L. wrote:

> On 16 August 2010 06:05, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi,
> > 
> > Here’s a situation:  
> > A product belongs to multiple categories and the categories are top  
> > menus of a site.
> 
> That does not make sense, do you mean product has\_many categories, or  
> more likely category has\_many products and product belongs\_to  
> category.

Sorry.  
I was not clear about this.  
Product and Category are m:n relationship.  
Actually there’s a Categoryship model.

> > I want to highlight the category of a product when the product detail  
> > page is loaded.  
> > If there’s some context, I can determine which category should be  
> > highlighted.  
> > (For example, I can track the categories that a user selected).  
> > But sometimes there’s no context (for example, the user visits the page  
> > directly without getting through categories).
> 
> If you have a product, @product say, then the the category of the  
> product is @product.category, assuming you have setup the ActiveRecord  
> relationships correctly.
> 
> Colin

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 16, 2010, 6:44pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-solve-this/193336/4 "2010-08-16T18:44:46Z")

</div>

On 16 August 2010 17:10, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > category.
> 
> Sorry.  
> I was not clear about this.  
> Product and Category are m:n relationship.  
> Actually there’s a Categoryship model.

It would be easier if you could just supply the relationships. I  
think you are saying category has\_many products through categoryship  
and product has\_many categories through categoryship also. In that  
case if you have a product then the categories are available as  
@product.categories. I am not sure what your question is.

Colin

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 16, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-solve-this/193336/5 "2010-08-16T19:19:45Z")

</div>

Hassan S. wrote:

> On Mon, Aug 16, 2010 at 10:02 AM, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > The problem is that @product.categories is multiple.  
> > I need to highlight only 1 category for the product and show only 1  
> > breadcrumb.  
> > If there’s some path that the user took, I can select the category but  
> > if there’s no such context, I don’t know what to do.  
> > I can make my own policy but I want to know how other developers are  
> > doing.
> 
> So instead of  
> _products_ \> _category_ \> product
> 
> you could do  
> _products_ \> { multiple categories } \> product
> 
> where the { multiple categories } is a mouseover dropdown to show  
> the list of relevant category links.

Yes, that will do.  
Our top menus are categories.  
I need to highlight the category.  
Do you have any good idea for that?

Thanks.  
Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 16, 2010, 7:17pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-solve-this/193336/6 "2010-08-16T19:17:33Z")

</div>

On Mon, Aug 16, 2010 at 10:02 AM, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The problem is that @product.categories is multiple.  
> I need to highlight only 1 category for the product and show only 1  
> breadcrumb.  
> If there’s some path that the user took, I can select the category but  
> if there’s no such context, I don’t know what to do.  
> I can make my own policy but I want to know how other developers are  
> doing.

So instead of  
_products_ \> _category_ \> product

you could do  
_products_ \> { multiple categories } \> product

where the { multiple categories } is a mouseover dropdown to show  
the list of relevant category links.

## FWIW,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 16, 2010, 7:02pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-solve-this/193336/7 "2010-08-16T19:02:25Z")

</div>

Colin L. wrote:

> On 16 August 2010 17:10, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > category.
> > 
> > Sorry.  
> > I was not clear about this.  
> > Product and Category are m:n relationship.  
> > Actually there’s a Categoryship model.
> 
> It would be easier if you could just supply the relationships. I  
> think you are saying category has\_many products through categoryship  
> and product has\_many categories through categoryship also. In that  
> case if you have a product then the categories are available as  
> @product.categories. I am not sure what your question is.

You are understanding the relationships correctly.  
The problem is that @product.categories is multiple.  
I need to highlight only 1 category for the product and show only 1  
breadcrumb.  
If there’s some path that the user took, I can select the category but  
if there’s no such context, I don’t know what to do.  
I can make my own policy but I want to know how other developers are  
doing.

Thanks.

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 16, 2010, 7:33pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-solve-this/193336/8 "2010-08-16T19:33:57Z")

</div>

On Mon, Aug 16, 2010 at 10:19 AM, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Our top menus are categories.  
> I need to highlight the category.  
> Do you have any good idea for that?

Well, some choices are to “highlight”

1. none of the categories
2. all of the relevant categories equally
3. one, based on some heuristic like “most used to access this product”
4. all relevant categories with differential highlighting (i.e. visual  
ranking)  
using the measure from #3

I hesitate suggesting one without seeing the whole thing in context 🙂

## HTH,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 16, 2010, 8:29pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-solve-this/193336/9 "2010-08-16T20:29:22Z")

</div>

Hassan S. wrote:

> On Mon, Aug 16, 2010 at 10:19 AM, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Our top menus are categories.  
> > I need to highlight the category.  
> > Do you have any good idea for that?
> 
> Well, some choices are to “highlight”
> 
> 1. none of the categories
> 2. all of the relevant categories equally
> 3. one, based on some heuristic like “most used to access this product”
> 4. all relevant categories with differential highlighting (i.e. visual  
> ranking)  
> using the measure from #3
> 
> I hesitate suggesting one without seeing the whole thing in context 🙂
> 
> ## HTH,
> 
> Hassan S. ------------------------ removed\_email\_address@domain.invalid  
> twitter: @hassan

You were very helpful.  
Thanks.

Sam
