jackc / surus

PostgreSQL extensions for ActiveRecord
MIT License
394 stars 35 forks source link

select with find_json and all_json #13

Closed mathieumahe closed 9 years ago

mathieumahe commented 9 years ago

When running

Message.select([:id, :author_id]).find_json(732)

or

Message.select([:id, :author_id]).find_json(732, columns: [:author_id])

I have a strange result (due to the presence of the select scope). For exemple, the second query creates the following sql statement : select row_to_json(t) from (SELECT "messages"."id", "messages"."author_id", "messages"."author_id" FROM "messages" WHERE "messages"."id" = 732) t and the following json : "{\"id\":732,\"author_id\":11992,\"author_id\":11992}"

I think, the find_json and all_json should be based on the select field if any. Idem for the include maybe ?

Awesome gem, by the way, I love the idea of creating json from the db!

jackc commented 9 years ago

That might be possible. It's hard to say without digging into the bowels of arel again. Is there something that using the select scope rather than the columns option to find_json gives you?

mathieumahe commented 9 years ago

It would be easier to use with other gem, like has_scope, inherited_resource or datagrid.

It would also be nicer in controllers responding to html and json. The code would be DRYer!

mathieumahe commented 9 years ago

And it will work with (default_)scope without duplications!