I am looking for a better method with designing some Rails views that require a lot of controller data shared across various models. It is often that i come across a scenario where i need to combine an association with various other models. Think of this scenario (this comes from a game):
- A city has many buildings (user has many buildings through city_buildings)
- Now, each building can be upgraded based on requirements. Let's say that in order to build a fire station, you first need to already have a well.
- My application shows the currently built buildings. It iterates through @city.buildings, along with their requirements, if they cannot be built.
Now, my problem is handling this data properly in my controller/view. I always try to use as fewer code inside my views as possible and try to use instance variables mainly there. Each building has different requirements that originate from the association building.requirements. Part of my view depends on this requirements, and i therefore have to have checks like this all over my view :
<% if building.fulfil_requirements?(city) %>
In the past, I've tried using SQL joins in order to create instance variables that have more information about an association, based on other models as well, but i find this technique quite messy. There are some occasions where the need to calculate something inside a foreach loop has me running a model method inside a view, which is something i really really not like.
So, I am wondering whether there's a better solution for combining multiple models behavior inside my views, without having to resort to adding logic inside my views.