Datamapper" Model.all()"方法创建3个选择而不选择正确的字段

时间:2015-06-15 13:49:11

标签: ruby-on-rails ruby sinatra padrino ruby-datamapper

我已经开始在Padrino中使用Datamapper,我有一些奇怪的问题。 问题解释如下:

1.代码:

 content_type :json
 @fonts = Font.all(:fields=>[:id,:name,:class_name])
 @fonts.to_json
  1. Datamapper执行3个查询并返回包含所有字段的所有结果。
  2. 请参阅执行的sql查询:

    DEBUG - (0.000087)SELECT idnameclass_name FROM fonts ORDER BY id

    DEBUG - (0.000205)SELECT idpost_script_namedesignerlicensecategoryfull_namestyleweightfilenamecopyrightsystem_file_pathhttp_pathext FROM fonts ORDER BY id < / p>

    DEBUG - (0.000158)SELECT idfont_face FROM fonts ORDER BY id

    请帮助我 它为什么会发生?

    提前谢谢!

1 个答案:

答案 0 :(得分:0)

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