There’s a part of my application where a table of contacts is displayed and each of the contact’s name and (many to many) tags are displayed. I have one big query to load in the data (in one query instead of a bunch). Then, the data is further filtered through session variables set by the user.
Each contact can have many tags.
contacts = contacts.
joins{tags.outer}.
group('contacts.id').
select(
"contacts.name,
array_to_string(array_agg(tags.name), ',') as tag_list,
)
filter_tags = session[:filter_tags] if session[:filter_tags]
if filter_tags
contacts = contacts.where{(taggings.tag_id >> filter_tags)
end
The list of tags is displayed for each contact with:
"#{contact.tag_list.split(',').uniq.join(", ")}"
When the session[filter] is not set, the table works great. It displays all of a contact’s tags.
However, when the session[filter] is set, the table only displays the tag for which it is filtering. I would like it to only show contacts tagged with the selected tags, but for those contacts, show all the other tags they are tagged with. Each contact should already have the whole list of tags aggregated as tag_list, but that is not the case.
My only guess is that rails is Lazy Evaluating (?), and so only when it must does it call all the joins and wheres all at once, so the filter_tags part happens at the same time as the initial query.
Is this the case? How can i force an evaluation so that the original tags are not filtered away? Is there another/better approach?