21 Mar 2013, 12:28
Generic-user-small

Nadeem B (2 posts)

Hi, please help. I am unable to get the same result as the output printed on p175 (pdf) following running the curl command (curl –silent http://localhost:3000/products/3/who_bought.atom).

I am using ruby 1.9.3 and Rails 4.0 beta1 on Windows.

The output i receive (hopefully it is readable!), seems to be a some exception thrown, i just dont know why or where! - “Action Controller: Exception caught” :-

C:\Nadeem\ruby_scripts\curl>curl –silent http://localhost:3000/products/3/who_bought.atom <!DOCTYPE html> <html lang="en">

Action Controller: Exception caught body { background-color: #FAFAFA; color: #333; margin: 0px; } body, p, ol, ul, td { font-family: helvetica, verdana, arial, sans-serif; font-size: 13px; line-height: 18px; } pre { font-size: 11px; white-space: pre-wrap; } pre.box { border: 1px solid #EEE; padding: 10px; margin: 0px; width: 958px; } header { color: #F0F0F0; background: #C52F24; padding: 0.5em 1.5em; } h2 { color: #C52F24; line-height: 25px; } .details { border: 1px solid #D0D0D0; border-radius: 4px; margin: 1em 0px; display: block; width: 978px; } .summary { padding: 8px 15px; border-bottom: 1px solid #D0D0D0; display: block; } .details pre { margin: 5px; border: none; } #container { box-sizing: border-box; width: 100%; padding: 0 1.5em; } .source * { margin: 0px; padding: 0px; } .source { border: 1px solid #D9D9D9; background: #ECECEC; width: 978px; } .source pre { padding: 10px 0px; border: none; } .source .data { font-size: 80%; overflow: auto; background-color: #FFF; } .info { padding: 0.5em; } .source .data .line_numbers { background-color: #ECECEC; color: #AAA; padding: 1em .5em; border-right: 1px solid #DDD; text-align: right; } .line { padding-left: 10px; } .line:hover { background-color: #F6F6F6; } .line.active { background-color: #FFCCCC; } a { color: #980905; } a:visited { color: #666; } a:hover { color: #C52F24; }

ActiveRecord::StatementInvalid in ProductsController#who_bought

SQLite3::SQLException: near "at": syntax error: SELECT "orders".* FROM "orders" INNER JOIN "line orders"."id" = "line_items"."order_id" WHERE "line_items"."product_id" = ? ORDER B T 1

Extracted source (around line #67):

65
66
67
68
69
70
          
~~~
def who_bought
@product = Product.find(params[:id])
@latest_order = @product.orders.order('updated at').last
if stale?(@latest_order)
respond_to do |format|
format.atom
~~~

Rails.root: C:/Nadeem/ruby_scripts/depot2

<a href="#" onclick="hide(&#39;Framework-Trace&#39;);hide(&#39;Full-Trace&#39;);show(&#39;Application-Trace&#39;);; return false;">App
<a href="#" onclick="hide(&#39;Application-Trace&#39;);hide(&#39;Full-Trace&#39;);show(&#39;Framework-Trace&#39;);; return false;">Fra
<a href="#" onclick="hide(&#39;Application-Trace&#39;);hide(&#39;Framework-Trace&#39;);show(&#39;Full-Trace&#39;);; return false;">Ful

<div id="Application-Trace" style="display: block;">
app/controllers/products_controller.rb:67:in `who_bought&#39;
</div>
<div id="Framework-Trace" style="display: none;">
sqlite3-1.3.7-x86 (mingw32) lib/sqlite3/database.rb:91:in `initialize&#39;
sqlite3-1.3.7-x86 (mingw32) lib/sqlite3/database.rb:91:in `new&#39;
sqlite3-1.3.7-x86 (mingw32) lib/sqlite3/database.rb:91:in `prepare&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/sqlite3_adapter.rb:303:in `block in exec_query&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract_adapter.rb:334:in `block in log&#39;
activesupport (4.0.0.beta1) lib/active_support/notifications/instrumenter.rb:20:in `instrument&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract_adapter.rb:329:in `log&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/sqlite3_adapter.rb:292:in `exec_query&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/sqlite3_adapter.rb:504:in `select&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `block in select_all&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/query_cache.rb:76:in `cache_sql&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `select_all&#39;
activerecord (4.0.0.beta1) lib/active_record/querying.rb:36:in `find_by_sql&#39;
activerecord (4.0.0.beta1) lib/active_record/relation.rb:555:in `exec_queries&#39;
activerecord (4.0.0.beta1) lib/active_record/relation.rb:447:in `load&#39;
activerecord (4.0.0.beta1) lib/active_record/relation.rb:197:in `to_a&#39;
activerecord (4.0.0.beta1) lib/active_record/relation/finder_methods.rb:327:in `find_last&#39;
activerecord (4.0.0.beta1) lib/active_record/relation/finder_methods.rb:123:in `last&#39;
activerecord-deprecated_finders (0.0.3) lib/active_record/deprecated_finders/relation.rb:146:in `last&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/implicit_render.rb:4:in `send_action&#39;
actionpack (4.0.0.beta1) lib/abstract_controller/base.rb:189:in `process_action&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/rendering.rb:10:in `process_action&#39;
actionpack (4.0.0.beta1) lib/abstract_controller/callbacks.rb:18:in `block in process_action&#39;
activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:413:in `_run__29697136__process_action__callbacks&#39;
activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:78:in `run_callbacks&#39;
actionpack (4.0.0.beta1) lib/abstract_controller/callbacks.rb:17:in `process_action&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/rescue.rb:29:in `process_action&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action&#39;
activesupport (4.0.0.beta1) lib/active_support/notifications.rb:158:in `block in instrument&#39;
activesupport (4.0.0.beta1) lib/active_support/notifications/instrumenter.rb:20:in `instrument&#39;
activesupport (4.0.0.beta1) lib/active_support/notifications.rb:158:in `instrument&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/instrumentation.rb:30:in `process_action&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/params_wrapper.rb:245:in `process_action&#39;
activerecord (4.0.0.beta1) lib/active_record/railties/controller_runtime.rb:18:in `process_action&#39;
actionpack (4.0.0.beta1) lib/abstract_controller/base.rb:136:in `process&#39;
actionpack (4.0.0.beta1) lib/abstract_controller/rendering.rb:44:in `process&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal.rb:196:in `dispatch&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal.rb:232:in `block in action&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/routing/route_set.rb:78:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/routing/route_set.rb:78:in `dispatch&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/routing/route_set.rb:46:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/journey/router.rb:69:in `block in call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/journey/router.rb:57:in `each&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/journey/router.rb:57:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/routing/route_set.rb:651:in `call&#39;
rack (1.5.2) lib/rack/etag.rb:23:in `call&#39;
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call&#39;
rack (1.5.2) lib/rack/head.rb:11:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/params_parser.rb:27:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/flash.rb:241:in `call&#39;
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context&#39;
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/cookies.rb:452:in `call&#39;
activerecord (4.0.0.beta1) lib/active_record/query_cache.rb:36:in `call&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/connection_pool.rb:632:in `call&#39;
activerecord (4.0.0.beta1) lib/active_record/migration.rb:348:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call&#39;
activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:373:in `_run__615768018__call__callbacks&#39;
activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:78:in `run_callbacks&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/callbacks.rb:27:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/reloader.rb:64:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call&#39;
railties (4.0.0.beta1) lib/rails/rack/logger.rb:38:in `call_app&#39;
railties (4.0.0.beta1) lib/rails/rack/logger.rb:21:in `block in call&#39;
activesupport (4.0.0.beta1) lib/active_support/tagged_logging.rb:67:in `block in tagged&#39;
activesupport (4.0.0.beta1) lib/active_support/tagged_logging.rb:25:in `tagged&#39;
activesupport (4.0.0.beta1) lib/active_support/tagged_logging.rb:67:in `tagged&#39;
railties (4.0.0.beta1) lib/rails/rack/logger.rb:21:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/request_id.rb:21:in `call&#39;
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call&#39;
rack (1.5.2) lib/rack/runtime.rb:17:in `call&#39;
activesupport (4.0.0.beta1) lib/active_support/cache/strategy/local_cache.rb:72:in `call&#39;
rack (1.5.2) lib/rack/lock.rb:17:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/static.rb:64:in `call&#39;
railties (4.0.0.beta1) lib/rails/engine.rb:510:in `call&#39;
railties (4.0.0.beta1) lib/rails/application.rb:96:in `call&#39;
rack (1.5.2) lib/rack/lock.rb:17:in `call&#39;
rack (1.5.2) lib/rack/content_length.rb:14:in `call&#39;
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service&#39;
C:/Nadeem/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service&#39;
C:/Nadeem/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run&#39;
C:/Nadeem/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread&#39;
</div>
<div id="Full-Trace" style="display: none;">
sqlite3-1.3.7-x86 (mingw32) lib/sqlite3/database.rb:91:in `initialize&#39;
sqlite3-1.3.7-x86 (mingw32) lib/sqlite3/database.rb:91:in `new&#39;
sqlite3-1.3.7-x86 (mingw32) lib/sqlite3/database.rb:91:in `prepare&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/sqlite3_adapter.rb:303:in `block in exec_query&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract_adapter.rb:334:in `block in log&#39;
activesupport (4.0.0.beta1) lib/active_support/notifications/instrumenter.rb:20:in `instrument&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract_adapter.rb:329:in `log&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/sqlite3_adapter.rb:292:in `exec_query&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/sqlite3_adapter.rb:504:in `select&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `block in select_all&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/query_cache.rb:76:in `cache_sql&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `select_all&#39;
activerecord (4.0.0.beta1) lib/active_record/querying.rb:36:in `find_by_sql&#39;
activerecord (4.0.0.beta1) lib/active_record/relation.rb:555:in `exec_queries&#39;
activerecord (4.0.0.beta1) lib/active_record/relation.rb:447:in `load&#39;
activerecord (4.0.0.beta1) lib/active_record/relation.rb:197:in `to_a&#39;
activerecord (4.0.0.beta1) lib/active_record/relation/finder_methods.rb:327:in `find_last&#39;
activerecord (4.0.0.beta1) lib/active_record/relation/finder_methods.rb:123:in `last&#39;
activerecord-deprecated_finders (0.0.3) lib/active_record/deprecated_finders/relation.rb:146:in `last&#39;
app/controllers/products_controller.rb:67:in `who_bought&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/implicit_render.rb:4:in `send_action&#39;
actionpack (4.0.0.beta1) lib/abstract_controller/base.rb:189:in `process_action&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/rendering.rb:10:in `process_action&#39;
actionpack (4.0.0.beta1) lib/abstract_controller/callbacks.rb:18:in `block in process_action&#39;
activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:413:in `_run__29697136__process_action__callbacks&#39;
activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:78:in `run_callbacks&#39;
actionpack (4.0.0.beta1) lib/abstract_controller/callbacks.rb:17:in `process_action&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/rescue.rb:29:in `process_action&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action&#39;
activesupport (4.0.0.beta1) lib/active_support/notifications.rb:158:in `block in instrument&#39;
activesupport (4.0.0.beta1) lib/active_support/notifications/instrumenter.rb:20:in `instrument&#39;
activesupport (4.0.0.beta1) lib/active_support/notifications.rb:158:in `instrument&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/instrumentation.rb:30:in `process_action&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/params_wrapper.rb:245:in `process_action&#39;
activerecord (4.0.0.beta1) lib/active_record/railties/controller_runtime.rb:18:in `process_action&#39;
actionpack (4.0.0.beta1) lib/abstract_controller/base.rb:136:in `process&#39;
actionpack (4.0.0.beta1) lib/abstract_controller/rendering.rb:44:in `process&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal.rb:196:in `dispatch&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch&#39;
actionpack (4.0.0.beta1) lib/action_controller/metal.rb:232:in `block in action&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/routing/route_set.rb:78:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/routing/route_set.rb:78:in `dispatch&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/routing/route_set.rb:46:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/journey/router.rb:69:in `block in call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/journey/router.rb:57:in `each&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/journey/router.rb:57:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/routing/route_set.rb:651:in `call&#39;
rack (1.5.2) lib/rack/etag.rb:23:in `call&#39;
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call&#39;
rack (1.5.2) lib/rack/head.rb:11:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/params_parser.rb:27:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/flash.rb:241:in `call&#39;
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context&#39;
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/cookies.rb:452:in `call&#39;
activerecord (4.0.0.beta1) lib/active_record/query_cache.rb:36:in `call&#39;
activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/connection_pool.rb:632:in `call&#39;
activerecord (4.0.0.beta1) lib/active_record/migration.rb:348:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call&#39;
activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:373:in `_run__615768018__call__callbacks&#39;
activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:78:in `run_callbacks&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/callbacks.rb:27:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/reloader.rb:64:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call&#39;
railties (4.0.0.beta1) lib/rails/rack/logger.rb:38:in `call_app&#39;
railties (4.0.0.beta1) lib/rails/rack/logger.rb:21:in `block in call&#39;
activesupport (4.0.0.beta1) lib/active_support/tagged_logging.rb:67:in `block in tagged&#39;
activesupport (4.0.0.beta1) lib/active_support/tagged_logging.rb:25:in `tagged&#39;
activesupport (4.0.0.beta1) lib/active_support/tagged_logging.rb:67:in `tagged&#39;
railties (4.0.0.beta1) lib/rails/rack/logger.rb:21:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/request_id.rb:21:in `call&#39;
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call&#39;
rack (1.5.2) lib/rack/runtime.rb:17:in `call&#39;
activesupport (4.0.0.beta1) lib/active_support/cache/strategy/local_cache.rb:72:in `call&#39;
rack (1.5.2) lib/rack/lock.rb:17:in `call&#39;
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/static.rb:64:in `call&#39;
railties (4.0.0.beta1) lib/rails/engine.rb:510:in `call&#39;
railties (4.0.0.beta1) lib/rails/application.rb:96:in `call&#39;
rack (1.5.2) lib/rack/lock.rb:17:in `call&#39;
rack (1.5.2) lib/rack/content_length.rb:14:in `call&#39;
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service&#39;
C:/Nadeem/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service&#39;
C:/Nadeem/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run&#39;
C:/Nadeem/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread&#39;
</div>

Request

Parameters:

{&quot;id&quot;=&gt;&quot;3&quot;,
 &quot;format&quot;=&gt;&quot;atom&quot;}

Toggle session dump</div> <div id="session_dump" style="display:none"> ~~~

</div>
</div>

<div class="details">
  <div class="summary"><a href="#" onclick="return toggleEnvDump()">Toggle env dump</a></div>
  <div id="env_dump" style="display:none">

GATEWAY_INTERFACE: "CGI/1.1" HTTP_ACCEPT: "/" REMOTE_ADDR: "127.0.0.1" REMOTE_HOST: "127.0.0.1" SERVER_NAME: "localhost" SERVER_PROTOCOL: "HTTP/1.1" ~~~

Response

Headers:

None

</html>

21 Mar 2013, 16:43
Samr_small_pragsmall

Sam Ruby (584 posts)

It looks like in your source, line 39 of app/controllers/products.rb is:

@latest_order = @product.orders.order('updated at').last

The correct code should read:

@latest_order = @product.orders.order('updated_at').last

Note that the space in the string is replaced by an underscore.

To make this a bit less error prone in the future, I’m going to update the book to suggest the following instead:

@latest_order = @product.orders.order(:updated_at).last
25 Mar 2013, 14:19
Generic-user-small

Nadeem B (2 posts)

I didn’t spot that.. Thanks for your help Sam much appreciated!

  You must be logged in to comment