# N00b: heterogeneous instances in views: any ideas?

**URL:** <https://www.ruby-forum.com/t/n00b-heterogeneous-instances-in-views-any-ideas/69428>\
**Category:** Rails\
**Created:** [September 20, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/n00b-heterogeneous-instances-in-views-any-ideas/69428 "2006-09-20T23:32:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thechak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aeb1de/32.png) [@thechak](https://www.ruby-forum.com/u/thechak)\
**Post date:** [September 20, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/n00b-heterogeneous-instances-in-views-any-ideas/69428/1 "2006-09-20T23:32:07Z")

</div>

Hey all –

This is a problem I’ve been banging my head against for a week or so,  
because Rails doesn’t provide a decent way to use inheritance in one’s  
model very well.

Let’s suppose I have the following class definitions:

class Person \< ActiveRecord::Base  
attr\_accessible :name  
attr\_accessor :name  
end

class Employee \< Person  
attr\_accessible :serial\_number  
attr\_accessor :serial\_number  
end

class Manager \< Employee  
attr\_accessible :group\_name  
attr\_accessor :group\_name  
end

I’d like for a view to display all the people in the system, but only  
display, for example, group\_names for those people who are also  
managers. This would come in handy if I wanted to show a table of all  
employees, for example.

The only decent way I can come up with is to use helper modules to do  
the dirty work of rendering. (Not too disgusting, but I wish the helper  
modules could just be classes, so I could use inheritance here too. I’d  
rather not have helper modules replicate the inheritance tree, e.g.  
forcing the ManagerHelper module to know that it must call the  
EmployeeHelper module’s methods. I’d rather just use ‘super’.)

The problem is that I’m now stuck either 1) adding information and the  
appropriate reader method to my model classes about which columns to  
display (e.g. let’s show ‘serial\_number’, but not ‘hashed\_password’),  
and in what order (e.g name first, then serial\_number), or 2)  
delegating this to other classses, which will be by their very  
definition coupled to the model classes – almost as ugly IMO.

I’ve looked at the Streamlined framework, but it doesn’t quite  
accomplish what I want.

Has anyone tried to do something like this? What’s the best way to  
approach this issue?

---

<div class="post-metadata">

**Author:** ![thechak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aeb1de/32.png) [@thechak](https://www.ruby-forum.com/u/thechak)\
**Post date:** [September 20, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/n00b-heterogeneous-instances-in-views-any-ideas/69428/2 "2006-09-20T23:46:08Z")

</div>

On 2006-09-20, at 18:25 , Dan C. wrote:

> I’d like for a view to display all the people in the system, but only  
> display, for example, group\_names for those people who are also  
> managers. This would come in handy if I wanted to show a table of all  
> employees, for example.

Have you considered using respond\_to?(:group\_names) ?

---

<div class="post-metadata">

**Author:** ![thechak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aeb1de/32.png) [@thechak](https://www.ruby-forum.com/u/thechak)\
**Post date:** [September 21, 2006, 1:04am UTC](https://www.ruby-forum.com/t/n00b-heterogeneous-instances-in-views-any-ideas/69428/3 "2006-09-21T01:04:14Z")

</div>

> I’d like for a view to display all the people in the system, but only  
> display, for example, group\_names for those people who are also  
> managers. This would come in handy if I wanted to show a table of all  
> employees, for example.

You could create separate partials \_employee.rhtml, \_manager.rhtml and  
use them to render partials based on the type of the model object

\<% for person in Person.find(:all) %\>  
\<%= render :partial=person.class.name.underscore,  
:locals=\>{:person=\>person} %\>  
\<% end %\>

Max

---

<div class="post-metadata">

**Author:** ![thechak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aeb1de/32.png) [@thechak](https://www.ruby-forum.com/u/thechak)\
**Post date:** [September 22, 2006, 4:23am UTC](https://www.ruby-forum.com/t/n00b-heterogeneous-instances-in-views-any-ideas/69428/4 "2006-09-22T04:23:50Z")

</div>

Ah, but I have another problem: I can’t seem to pass locals into  
partials.

This is truly maddening. My problems would otherwise be solved. In  
fact, I would  
probably put the code in your post in another file in the interest of  
DRYness, since  
it could be re-factored rather nicely.

Another issue I’d like to tackle is the way the attributes to render  
are distributed  
among the partials. I don’t necessarily want the \_employee.rhtml  
partial to know  
anything explicitly about the attributes of the base Person class. But  
I think this may  
be my problem… I think I’m stuck having the model classes telling the  
view code what  
to render.

---

<div class="post-metadata">

**Author:** ![thechak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aeb1de/32.png) [@thechak](https://www.ruby-forum.com/u/thechak)\
**Post date:** [September 22, 2006, 5:38am UTC](https://www.ruby-forum.com/t/n00b-heterogeneous-instances-in-views-any-ideas/69428/5 "2006-09-22T05:38:21Z")

</div>

Daniel N wrote:

> On 9/22/06, Dan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Ah, but I have another problem: I can’t seem to pass locals into  
> > partials.
> 
> render :partial =\> ‘my\_partial’, :locals =\> { :a\_local\_variable\_something =\>  
> my\_local }

Hey Dan,

Take a look at this… It has a few helper methods that I’ve found  
handy for making links and rendering partials when you don’t know what  
the item is ahead of time.

[http://www.sciwerks.com/blog/2006/05/24/polymorphic-partials/](http://www.sciwerks.com/blog/2006/05/24/polymorphic-partials/)

\_Kevin

---

<div class="post-metadata">

**Author:** ![thechak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aeb1de/32.png) [@thechak](https://www.ruby-forum.com/u/thechak)\
**Post date:** [September 22, 2006, 7:16am UTC](https://www.ruby-forum.com/t/n00b-heterogeneous-instances-in-views-any-ideas/69428/6 "2006-09-22T07:16:27Z")

</div>

Daniel N wrote:

> > 
> 
> Thanx for the link
> 
> Cheers

Yeah, it’s pretty robust. I think I wrote this before the simply  
helpful plugin came out.  
You can pass my method a collection of dissimilar objects and it will  
call the correct partial for each one. Sometimes that is a good idea,  
and sometimes it isn’t.

I also like being able to write links like this…

\<%= link\_to @item.name, to\_url(@item) %\>

\_Kevin

---

<div class="post-metadata">

**Author:** ![thechak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aeb1de/32.png) [@thechak](https://www.ruby-forum.com/u/thechak)\
**Post date:** [September 22, 2006, 6:18am UTC](https://www.ruby-forum.com/t/n00b-heterogeneous-instances-in-views-any-ideas/69428/7 "2006-09-22T06:18:23Z")

</div>

On 9/22/06, \_Kevin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That looks quite interesting and very handy.

Have you looked at the simply helpful plugin? It does similar things,  
but  
one thing I found that it doesn’t do that your method does is take care  
of  
STI. If you have a collection of different objects with simply helpful,  
the  
first object found specifies the partial for all, but I think your  
method  
would not suffer from this.

Thanx for the link

Cheers

---

<div class="post-metadata">

**Author:** ![thechak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aeb1de/32.png) [@thechak](https://www.ruby-forum.com/u/thechak)\
**Post date:** [September 28, 2006, 4:12am UTC](https://www.ruby-forum.com/t/n00b-heterogeneous-instances-in-views-any-ideas/69428/8 "2006-09-28T04:12:36Z")

</div>

Hi Kevin –

Thanks! This is very close to what I need.

The only other fly in the ointment (for me anyway) is how to render  
some  
attributes of instances but not others – in effect, circumventing the  
“union of  
all attributes” problem of STI.

The only real way I can see to do this is with a method in a given  
model classes  
that returns an array of symbols corresponding to the attributes of the  
class (using  
the order in the array as an optional presentation “hint”):

module Viewable  
def viewables  
self.attrs  
end  
end

class Employee \< Person  
include Viewable  
# probably should be private… I need to flesh this out…  
def self.attrs  
[:type, :name, :serial\_number]  
end  
end

I’m loathe to force this on the model, but that’s where the  
responsibility seems to lie.

---

<div class="post-metadata">

**Author:** ![thechak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aeb1de/32.png) [@thechak](https://www.ruby-forum.com/u/thechak)\
**Post date:** [September 22, 2006, 5:28am UTC](https://www.ruby-forum.com/t/n00b-heterogeneous-instances-in-views-any-ideas/69428/9 "2006-09-22T05:28:18Z")

</div>

On 9/22/06, Dan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ah, but I have another problem: I can’t seem to pass locals into  
> partials.

render :partial =\> ‘my\_partial’, :locals =\> {  
:a\_local\_variable\_something =\>  
my\_local }

See

> **[ActionController::Base](https://api.rubyonrails.org/classes/ActionController/Base.html#M000206for)**
>
> Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed on request and then either it renders a template or redirects to another action.

more information
