Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

properties_to_serialize doesn't use the query's projected fields #30

Open
jpi opened this issue Jun 11, 2012 · 0 comments
Open

properties_to_serialize doesn't use the query's projected fields #30

jpi opened this issue Jun 11, 2012 · 0 comments

Comments

@jpi
Copy link

jpi commented Jun 11, 2012

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant