# Models as "views" on another model

**URL:** <https://www.ruby-forum.com/t/models-as-views-on-another-model/171944>\
**Category:** Rails\
**Created:** [July 12, 2009, 12:20am UTC](https://www.ruby-forum.com/t/models-as-views-on-another-model/171944 "2009-07-12T00:20:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 12, 2009, 12:20am UTC](https://www.ruby-forum.com/t/models-as-views-on-another-model/171944/1 "2009-07-12T00:20:08Z")

</div>

I would like to selectively reuse some data. I have two groups of  
people who want very similar information about subject experts - but  
they want slightly different fields and/or want to see the other  
group’s information but should not be able to edit it.

If I create the following models, don’t the child models have access  
to all the parent’s attributes (unless I override the attribute  
accessors individually)?

class Expert \< ActiveRecord::Base

# has fields like name, email, title, expertise\_according\_to\_group1,

expertise\_according\_to\_group2  
end

class Group1\_Expert \< Expert

# If I did this, this model would have access to all fields in Expert,

right? Including the group2 fields  
end

I could do what I want by just ignoring the group2 fields when showing  
the expert to group1, but that feels kind of ummm unclean.

One can use database views to provide access controls on the  
underlying table. I would like to do essentially the same thing in  
Ruby/Rails, but I don’t know how.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 12, 2009, 1:46am UTC](https://www.ruby-forum.com/t/models-as-views-on-another-model/171944/2 "2009-07-12T01:46:16Z")

</div>

No, for models you would have to do this:

class Expert \< ActiveRecord::Base  
self.abstract\_class = true  
end

class YourChildClass \< Expert

# Inherits from Expert

end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 12, 2009, 1:48am UTC](https://www.ruby-forum.com/t/models-as-views-on-another-model/171944/3 "2009-07-12T01:48:09Z")

</div>

Ã„lphÃ¤ BlÃ¼Ã« wrote:

> No, for models you would have to do this:
> 
> class Expert \< ActiveRecord::Base  
> self.abstract\_class = true  
> end
> 
> class YourChildClass \< Expert
> 
> # Inherits from Expert
> 
> end

It is also a good practice to set the table names in the inherited  
models by doing:

class YourChildClass \< Expert  
set\_table\_name “your\_child\_classes”  
end

Make sure you pluralize the table name…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 12, 2009, 2:12am UTC](https://www.ruby-forum.com/t/models-as-views-on-another-model/171944/4 "2009-07-12T02:12:53Z")

</div>

On Jul 11, 11:19 pm, cnk [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # has fields like name, email, title, expertise\_according\_to\_group1,
> 
> expertise\_according\_to\_group2  
> end
> 
> class Group1\_Expert \< Expert
> 
> # If I did this, this model would have access to all fields in Expert,
> 
> right? Including the group2 fields  
> end
> 
> I could do what I want by just ignoring the group2 fields when showing  
> the expert to group1, but that feels kind of ummm unclean.

That is the drawback with single table inheritance, unless your  
subclasses have almost all of their data in common it gets a little  
icky. One approach is to move the data that is not common in to a  
separate set of tables (with the appropriate relationships)

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 12, 2009, 8:06pm UTC](https://www.ruby-forum.com/t/models-as-views-on-another-model/171944/5 "2009-07-12T20:06:34Z")

</div>

It’s not really practical to provide “access control” at the Ruby  
source code level. I’d hope that you’re not giving your users access  
to run code on the server, so this is really just a UI issue.

–Matt J.
