rails joins with select returns nil as id -
why joins query returns id null in last haven't query id. how should prevent that?
2.1.3 :038 > foo.select('bars.comment_1, bars.comment_2, foos.rating').joins(:bars).each |b| 2.1.3 :039 > p b.to_json 2.1.3 :040?> end foo load (0.3ms) select comment_1, comment_2, rating `foos` inner join `bars` on `bars`.`foo_id` = `foos`.`id` "{\"comment_1\":\"xyz\",\"comment_2\":\"uvw\",\"rating\":\"good\",\"id\":null}" "{\"comment_1\":\"xyz\",\"comment_2\":\"uvw\",\"rating\":\"good\",\"id\":null}" "{\"comment_1\":\"xyz\",\"comment_2\":\"uvw\",\"rating\":\"fair\",\"id\":null}" "{\"comment_1\":\"xyz\",\"comment_2\":\"uvw\",\"rating\":\"poor\",\"id\":null}" "{\"comment_1\":\"xyz\",\"comment_2\":\"uvw\",\"rating\":\"good\",\"id\":null}"
you should use only
option of to_json
method:
foo.select('bars.comment_1, bars.comment_2, foos.rating').joins(:bars).each |b| p b.to_json(only: [:comment_1, :comment_2, :rating]) end
Comments
Post a Comment