subtree_ids already includes param.reference_id, you can write:

 conditions << "hosts.hostgroup_id IN (#{param.hostgroup.subtree_ids.join(', ')})"