Split your query into two. In the first find the max revision for each name. In the second query find the full row which matches name and revision.
CREATE TEMP TABLE max_revisions (name varchar, revision integer);
INSERT INTO max_revisions SELECT name, max(revision) FROM revisions
WHERE saved = 1 GROUP BY name;
SELECT r.* FROM revisions r INNER JOIN max_revisions m
ON m.name = r.name AND m.revision = r.revision;
Now your problem may be, how to express this in Rails.
You may use Revision.connection.execute and then Revision.find_by_sql (both wrapped in a single method, for example: Revision.find_by_max_revisions).
Otherwise, if your database does not support temporary tables, or you just don't want them, you may read the max_revisions into memory, and then use it to build the query:
class Revision
def self.find_by_max_revisions
max_revs = connection.select_values(
sanitize_sql(
["SELECT name, max(revision) as max_rev FROM #{table_name}
WHERE saved = ? GROUP BY name", 1]
), "Load max_revisions for #{self.name}"
)
max_revs.map do |row|
find_by_name_and_revision(row["name"], row["max_rev"].to_i)
end
end
end