ruby on rails - Prevent the double query? -
my goal pull data sql database , split 2 arrays (req of library) can graph data. problem process creating 2 database queries instead of one.
in controller:
def words_popular words = word.select(:word, :popularity).order(popularity: :desc).limit(100) @words = words.pluck(:word) @popularity = words.pluck(:popularity) end
and resulting log:
(1.9ms) select `dictionary`.`word` `dictionary` order `dictionary`.`popularity` desc limit 100 (2.0ms) select `dictionary`.`popularity` `dictionary` order `dictionary`.`popularity` desc limit 100
is problem double plucking? can array split in 1 line?
also, in future, there way force query? in node mongoose, call exec() on relation cause run.
try this:
words, popularity = word.pluck(:word, :popularity).transpose
and words contains array of words , popularity array of popularity.
Comments
Post a Comment