Rails model design

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?

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 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.

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?