Datamapper "Model.all()" method creates 3 selects and not selecting the right fields

62 views Asked by At

I've started to use Datamapper with Padrino and i have some weird issue. The issues is explained below:

1.The code:

 content_type :json
 @fonts = Font.all(:fields=>[:id,:name,:class_name])
 @fonts.to_json
  1. Datamapper executes 3 queries and return me the all result with all fields.

Please see the sql queries that executed:

DEBUG - (0.000087) SELECT id, name, class_name FROM fonts ORDER BY id

DEBUG - (0.000205) SELECT id, post_script_name, designer, license, category, full_name, style, weight, filename, copyright, system_file_path, http_path, ext FROM fonts ORDER BY id

DEBUG - (0.000158) SELECT id, font_face FROM fonts ORDER BY id

Please help me with that Why it's happening ?

Thank you in advance!

1

There are 1 answers

0
Yaniv Vova Alterman On BEST ANSWER

The solution is:

 content_type :json
 fields_to_select = [:id,:name,:class_name]
 @fonts = Font.all(:fields => fields_to_select).to_json(:only=> fields_to_select)
 @fonts