# Rails model design

**URL:** <https://www.ruby-forum.com/t/rails-model-design/178977>\
**Category:** Rails\
**Created:** [November 14, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/rails-model-design/178977 "2009-11-14T17:21:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![introvert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c37758/32.png) [@introvert](https://www.ruby-forum.com/u/introvert)\
**Post date:** [November 14, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/rails-model-design/178977/1 "2009-11-14T17:21:46Z")

</div>

Hello.

I have a Plan model which has a type attribute and there should be more  
options for the setting by which I want to display different records  
(similar as categories)

I have a few concerns which would be the best way to design the  
structure of model.

Would create a new model PlanType and associate each Plan with it or  
would you just statically code the represented values in the application  
in such cases?

Would it be better to make a polymorphic association so that I could  
also use the relations betwen other model as well?

---

<div class="post-metadata">

**Author:** ![introvert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c37758/32.png) [@introvert](https://www.ruby-forum.com/u/introvert)\
**Post date:** [November 16, 2009, 2:33pm UTC](https://www.ruby-forum.com/t/rails-model-design/178977/2 "2009-11-16T14:33:41Z")

</div>

Hello Al,

> I have a Plan model which has a type attribute and there should be more  
> options for the setting by which I want to display different records  
> (similar as categories)

Do you mean, for example, that you could show, in different situations,  
only plans of the type A / only of type B / all the plans… and so on?

> Would create a new model PlanType and associate each Plan with it or  
> would you just statically code the represented values in the application  
> in such cases?

I don’t love the static representation in source code: Rails is, in the  
end, a database web-frontend, and, as far you can, you should always go  
through database. For example, what if the types/categories grow or  
decrease? This way you can also leverage the powerful helpers to create  
select block in your views.

> Would it be better to make a polymorphic association so that I could  
> also use the relations betwen other model as well?

This depends on the plans’ nature, and on the types’ one.

So, are these types really general and applicable to more than one  
model?  
For example: both plans and (say) project could be for example  
“started”, “work in progress”, “completed”?  
If so, you could go polymorphic, but be aware that you can’t directly  
traverse the association from the type to the objects of that type,  
because you would have an heterogeneous collection (that you could  
anyway “group\_by{}”). See  
[has\_many :through - The other side of polymorphic :through associations](http://blog.hasmanythrough.com/2006/4/3/polymorphic-through) about this  
topic.

Instead, in a more common case, the type could be targeted to a specific  
model (say, PlanType for the plans and ProjectType for the projects).  
This could be particular useful if different types had very different  
secondary attributes.

Luca B.

---

<div class="post-metadata">

**Author:** ![introvert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c37758/32.png) [@introvert](https://www.ruby-forum.com/u/introvert)\
**Post date:** [November 16, 2009, 3:27pm UTC](https://www.ruby-forum.com/t/rails-model-design/178977/3 "2009-11-16T15:27:52Z")

</div>

I was thinking that you could also consider using polymorphic routes.

Luca B.

Al F. wrote:

> Hello.
> 
> I have a Plan model which has a type attribute and there should be more  
> options for the setting by which I want to display different records  
> (similar as categories)
> 
> I have a few concerns which would be the best way to design the  
> structure of model.
> 
> Would create a new model PlanType and associate each Plan with it or  
> would you just statically code the represented values in the application  
> in such cases?
> 
> Would it be better to make a polymorphic association so that I could  
> also use the relations betwen other model as well?
