Cherry-pick 8c1e41c from https://github.com/TheHiveProjects/rails_sql_views