Datamapper allows to restrict the fields to fetch from database via the :fields option, for example :
MyModel.all(:fields => [:id, :name])
However, when serializing such a "restricted" collection, all the fields are serialized, causing new SQL queries to be sent to the database backend.
I find this counter-intuitive :-)
As a quick test, I tried to replace a line in lib/dm-serializer/common.rb :
--- lib/dm-serializer/common.rb.ori 2012-06-11 14:04:32.201075846 +1100
+++ lib/dm-serializer/common.rb 2012-06-11 14:08:03.326296260 +1100
@@ -12,7 +12,7 @@
only_properties = Array(options[:only])
excluded_properties = Array(options[:exclude])
- model.properties(repository.name).reject do |p|
+ query.fields.reject do |p|
if only_properties.include? p.name
false
else
... and it seems to work for me, both with projected fields and without.
But I'm not sure every DataMapper::Collection has a query.
Would you consider the original behaviour (serialize all model fields) to be expected, or a bug?