View Question... or is it a model question?

I have a partial, that generates a list of categories. I'd like to use the same partial for two different scenarios where data will be coming from different models. In one case, the partial is grabbing category data directly from the Category model. In another case it's grabbing it from an associated UserCategory model.

Based on the associations, in one scenario, i'd display the category name like this in the loop: category.name. In the other scenario (where I'm first retrieving category info through the UserCategory model, I'd display it like: category.category.name.

What's the best way to use one partial for both situations? Do I alias category name somehow in the model so it can work for both scenarios?

I look forward to your suggestions.

Thanks!

That's probably the 'cleanest' (since non-ugliest isn't really a word) option. The next big question is how far do you take it? How you actually implement it is where some people tend to like one way over the other.

You could simply alias the name to a delegated method and call it done.

Or, like in my day job, we actually have 'view state' objects that take much this kind of complexity out of the controllers and models which makes things a bit simpler and easier to test. They are designed to be loaded up and validated (via Validatable[0] and a Forwardable[1] extension, if needed), which is then passed along to the view such that the view does not need to care what kind of "category" it has or how to get the name from it.

Hope that helps.

[0] http://validatable.rubyforge.org/ [1] Jay Fields' Thoughts: Ruby: Forwardable addition

Thanks James, that was very helpful.

-Alex