Reputation: 2906
This question is related to my previous question, Creating a Rails partial that is model-agnostic.
I am trying to create a Rails partial that is given a variable, say, seasonal_type
, and does several different operations, such as querying a resource, and accessing instance variables, based on the value of the variable.
To be more concrete: I have the two models SafetyTest
and TeamDue
, which belong to Student
. In one particular case in this partial, I need to call either
@student.safety_tests
or @student.team_dues
. I would like to pass the variable seasonal_type
to the partial and like that determine which of the two is executed. How would I be able to do this? The value of the variable could be whatever is most convenient, such as a symbol (:safety_test
) or a model (SafetyTest
).
There is also another part to this question. In my controller, I precaculcated some instance variables for SafetyTest
and TeamDue
, such as @valid_safety_test
and @valid_team_due
. In my partial, how can I use either of these two instance variables, based on the value of seasonal_type
? Or should I just calculate these values inside the partial, even though that would be against the MVC structure?
Any help would be much appreciated.
Upvotes: 1
Views: 65
Reputation: 2906
Here is the answer:
In my controller, I created an array with the models of both seasonal types: [SafetyTest, TeamDue]
Then I iterated over this array, and used the model to query all the information that I needed from the database. I put that information into an array and passed it on to the view with an instance variable. So this array also has two items, one for SafetyTest
and one for TeamDue
.
Then the partial iterates through this array and renders the view necessary.
Upvotes: 1