intridea/grape — Issue #80: Grape::Entity breaks requesting a collection with a query scope
Author: bullfight
Created: 2011-11-24 02:03:23+00:00
State: open

Grape::Entity breaks Grape::API when requesting a collection with a query scope

Using entity on a collection with a query scope is throwing an error

the following works without issue

````ruby
resource :posts do
get do
@posts = Post.all
present @posts, :with => PostEntity
end
end

````

while

````ruby
resource :posts do
get do
@posts = Post.order('published_at DESC')
present @posts, :with => PostEntity
end
end
````

throws the following error

````
undefined local variable or method `published_at' for #
# ./spec/api/posts_api_spec.rb:12:in `block (3 levels) in '
````

With the following framework trace

````
activerecord (3.1.1) lib/active_record/relation.rb:459:in `method_missing'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/entity.rb:145:in `value_for'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/entity.rb:124:in `block in serializable_hash'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/entity.rb:123:in `each'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/entity.rb:123:in `inject'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/entity.rb:123:in `serializable_hash'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/base.rb:115:in `encode_json'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/formatter.rb:79:in `call'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/formatter.rb:79:in `block in after'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/formatter.rb:78:in `collect'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/formatter.rb:78:in `after'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/base.rb:26:in `call!'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/base.rb:19:in `call'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/base.rb:25:in `call!'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/base.rb:19:in `call'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/error.rb:44:in `block in call!'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/error.rb:43:in `catch'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/error.rb:43:in `call!'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/middleware/base.rb:19:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:68:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
/[...]/bundler/gems/grape-12e44781d7e8/lib/grape/api.rb:33:in `call'
rack-mount (0.8.3) lib/rack/mount/prefix.rb:26:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:532:in `call'
airbrake (3.0.4) lib/airbrake/rack.rb:27:in `call'
sass (3.1.10) lib/sass/plugin/rack.rb:54:in `call'
warden (1.0.6) lib/warden/manager.rb:35:in `block in call'
warden (1.0.6) lib/warden/manager.rb:34:in `catch'
warden (1.0.6) lib/warden/manager.rb:34:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.3.5) lib/rack/etag.rb:23:in `call'
rack (1.3.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/flash.rb:243:in `call'
rack (1.3.5) lib/rack/session/abstract/id.rb:195:in `context'
rack (1.3.5) lib/rack/session/abstract/id.rb:190:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/cookies.rb:331:in `call'
activerecord (3.1.1) lib/active_record/query_cache.rb:62:in `call'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (3.1.1) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/reloader.rb:68:in `call'
rack (1.3.5) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.1) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.5) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.5) lib/rack/lock.rb:15:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/static.rb:53:in `call'
airbrake (3.0.4) lib/airbrake/user_informer.rb:12:in `call'
railties (3.1.1) lib/rails/engine.rb:456:in `call'
railties (3.1.1) lib/rails/rack/content_length.rb:16:in `call'
railties (3.1.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.3.5) lib/rack/handler/webrick.rb:59:in `service'
/[...]/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/[...]/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/[...]/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
````

and a simplified version of the app setup I am using

````ruby
# app/models/post.rb

# == Schema Information
#
# Table name: lessons
#
# id :integer not null, primary key
# published_at :datetime
# title :string(255)
# body :text
# created_at :datetime
# updated_at :datetime

class Post < ActiveRecord::Base
end
````

````ruby
# app/api/posts_api.rb

class PostsApi < Grape::API
represent Post, :with => PostEntity
prefix "api"

resource :posts do
get do
@posts = Post.order('published_at DESC') #vs Post.all
present @posts, :with => PostEntity
end
end

end
````

````ruby
# app/entities/posts_entity.rb

class PostEntity < Grape::Entity
expose :title, :body, :published_at
end
````

````ruby
# config/routes.rb

mount PostsApi => "/"
````

````ruby
# config/application.rb

module myApp
class Application < Rails::Application
...
config.autoload_paths += %W(#{Rails.root}/api, #{Rails.root}/entities)
...
````