3036: Fix merge mistakes in collection_controller.
[arvados.git] / services / api / app / controllers / application_controller.rb
index 4a1a7d46dba7f1e7e2a765119c398ca96ae74685..c9c78794d62ca652e756e087f1840b875eec6d1e 100644 (file)
@@ -104,7 +104,9 @@ class ApplicationController < ActionController::Base
     if e.respond_to? :backtrace and e.backtrace
       logger.error e.backtrace.collect { |x| x + "\n" }.join('')
     end
-    if @object and @object.errors and @object.errors.full_messages and not @object.errors.full_messages.empty?
+    if (@object and @object.respond_to? :errors and
+        @object.errors and @object.errors.full_messages and
+        not @object.errors.full_messages.empty?)
       errors = @object.errors.full_messages
       logger.error errors.inspect
     else
@@ -203,7 +205,17 @@ class ApplicationController < ActionController::Base
       end
     end
 
-    @objects = @objects.select(@select.map { |s| "#{table_name}.#{ActiveRecord::Base.connection.quote_column_name s.to_s}" }.join ", ") if @select
+    if @select
+      # Map attribute names in @select to real column names, resolve
+      # those to fully-qualified SQL column names, and pass the
+      # resulting string to the select method.
+      api_column_map = model_class.attributes_required_columns
+      columns_list = @select.
+        flat_map { |attr| api_column_map[attr] }.
+        uniq.
+        map { |s| "#{table_name}.#{ActiveRecord::Base.connection.quote_column_name s}" }
+      @objects = @objects.select(columns_list.join(", "))
+    end
     @objects = @objects.order(@orders.join ", ") if @orders.any?
     @objects = @objects.limit(@limit)
     @objects = @objects.offset(@offset)