dewski/json_builder — Issue #8: Can't use ActiveRecord scopes directly
Author: lagartoflojo
Created: 2011-11-29 15:32:30+00:00
State: open

With a model like this:

```ruby
# app/models/child.rb
class Child < ActiveRecord::Base
validates_presence_of :name
def self.not_null # contrived example
where("name is not null")
end
end
```

And a template like this:

```ruby
# index.json.json_builder
children Child.not_null do |c|
id c.id
end
```

Rails throws:

```
undefined method `id' for #
```

This is fixed by adding ```.all``` to the scope call (i.e. ```where("name is not null").all```), but that would break chained scopes (i.e. ```Child.not_null.order_by_name```), so it's not a solution,.

I believe this error is line 13 in Member which asks ```if argument.is_a?(Array)```, to which an ActiveRecord::Relation returns false.

I guess the solution would be to check whether ```argument``` is "array-like" rather than an actual array (maybe something like ```if argument.respond_to? :each```, but that would catch Hashes and anything else that has an ```each``` method), or adding ```|| argument.is_a?(ActiveRecord::Relation)``` (but that would be a Rails-specific solution).