jackc / surus

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

all_json does not work with chaining first after where #6

Closed alubbe closed 11 years ago

alubbe commented 11 years ago

The following code

User.where("bla bla ? bla ?", x, y).first(30).all_json

breaks with the following error

undefined method `all_json' for #User:0x000000076a81d0

Could you take a look into this? Thanks!

alubbe commented 11 years ago

I am not an expert on activerecord yet, but here is my hunch: in json/model.rb, the all_json method is constructed in such a way that it expects to be called after "where". This disables all_json being called after any chain of activerecord methods. This could be circumvented by expanding the string inside where() but it goes against rails' readability. It would be great if you could dig deeper to see if it is possible to fix without too much hassle; if I can help you in any way, let me know!

jackc commented 11 years ago

all_json available on an ActiveRecord class or scope chain. first(30) actually fires the query and returns ActiveRecord objects. What you want is to replace first(30) with limit(30).