I have the following models in my Rails application:
class Test
has_many :tests_profiles
has_many :class_profiles
class TestsProfile
belongs_to :class_profile
belongs_to :test
class ClassProfile
has_many :tests_profiles
I must query tests belonging to particular ClassProfile. My present helper function is like this:
def get_tests(class_profile)
return Test.joins(:tests_profiles).where(tests_profiles: {class_profile_id: class_profile.id})
In my erb file, I am looping through the result like this:
<% tests = get_tests(class_profile) %>
<% tests.each do |test| %>
<th><%= test.name %></th>
<% end %>
But the problem here is I am getting all the tests names and not the only ones associated with that particular ClassProfile. How can I correct it so that it functions that way I want it to?
ClassProfile' - it sounds like all you need isclass_profile.tests.TestProfile. How can I do that?Testassociated many to one toClassProfile?has_many :tests_profilesbutclass TestProfile. Note difference betweentestandtests, it should be the same for both cases