Preview:
file = "#{Rails.root}/public/users.csv"
headers = ["Name", "Company Name", "Email", "Role", "Team Name"]
CSV.open(file, 'w', write_headers: true, headers: headers) do |writer|
    Team.all.each do |team|
      team.users.each do |user|
        writer << [user.name, user.company_name , user.email, user.roles&.first&.name, team.name]
      end
    end
  end
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter