YAML::load raises undefined class/module error - ruby-on-rails

I’m implementing paper trail like feature in my rails app. In order to do this, I’m serializing object in YAML. I’ve got item_at_version method, which basically does YAML::load(cached_object) – this works pretty well, however, I have no idea why, sometimes it returns undefined class/module _class name_.
It works with models like Event, Conversation, Note, and many more, but without any reason, it seems to throw that error for models like Dataset, Comment, Student (I’ve tried to find any pattern for that, without any luck).
I’m using rails 3.2.8, ruby 1.9.3p327, psych as YAML Engine (Psych::Version returns 1.3.4).
Ps. When I add require 'model_name' on top of that file, it works like a charm.
Any ideas what should I change/add to get this working?
Edit:
There’s no much code which I can share:
def item_at_version
YAML::load(cached_object)
end
But maybe backtrace would be interesting:
~/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:312:in `path2class'
~/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:312:in `resolve_class'
~/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:219:in `visit_Psych_Nodes_Mapping'
~/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/visitors/visitor.rb:15:in `visit'
~/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/visitors/visitor.rb:5:in `accept'
~/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:20:in `accept'
~/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:238:in `visit_Psych_Nodes_Document'
~/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/visitors/visitor.rb:15:in `visit'
~/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/visitors/visitor.rb:5:in `accept'
~/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:20:in `accept'
~/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/nodes/node.rb:35:in `to_ruby'
~/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych.rb:128:in `load'
app/models/history_version.rb:7:in `item_at_version'
app/controllers/history_controller.rb:8:in `show'
actionpack (3.2.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.8) lib/active_support/callbacks.rb:502:in `_run__1697733322876708236__process_action__1122943786273335015__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.8) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.8) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:600:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/cookies.rb:339:in `call'
activerecord (3.2.8) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `_run__2589517259026276185__call__1369641113040304056__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
bugsnag (1.2.5) lib/bugsnag/rack.rb:35:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
quiet_assets (1.0.1) lib/quiet_assets.rb:20:in `call_with_quiet_assets'
actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.8) lib/rails/engine.rb:479:in `call'
railties (3.2.8) lib/rails/application.rb:223:in `call'
railties (3.2.8) lib/rails/railtie/configurable.rb:30:in `method_missing'
~/Library/Application Support/Pow/Versions/0.4.0/node_modules/nack/lib/nack/server.rb:147:in `handle'
~/Library/Application Support/Pow/Versions/0.4.0/node_modules/nack/lib/nack/server.rb:99:in `rescue in block (2 levels) in start'
~/Library/Application Support/Pow/Versions/0.4.0/node_modules/nack/lib/nack/server.rb:96:in `block (2 levels) in start'
~/Library/Application Support/Pow/Versions/0.4.0/node_modules/nack/lib/nack/server.rb:86:in `each'
~/Library/Application Support/Pow/Versions/0.4.0/node_modules/nack/lib/nack/server.rb:86:in `block in start'
~/Library/Application Support/Pow/Versions/0.4.0/node_modules/nack/lib/nack/server.rb:66:in `loop'
~/Library/Application Support/Pow/Versions/0.4.0/node_modules/nack/lib/nack/server.rb:66:in `start'
~/Library/Application Support/Pow/Versions/0.4.0/node_modules/nack/lib/nack/server.rb:13:in `run'
~/Library/Application Support/Pow/Versions/0.4.0/node_modules/nack/bin/nack_worker:4:in `<main>'

When you use YAML.dump to serialize an object in Ruby, the class name is use as part of the Yaml tag so that the correct class can be used when loading the object. For example:
require 'yaml'
class Foo; end
puts YAML.dump Foo.new
produces
--- !ruby/object:Foo {}
When you use YAML.load on that string, Psych knows what class to instantiate for the deserialized object.
If you try to call YAML.load on a Yaml string that specifies a class that hasn’t been defined, then you will get the error:
require 'yaml'
# No Bar class has been defined
YAML.load '--- !ruby/object:Bar {}'
produces:
/Users/matt/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:312:in `path2class': undefined class/module Bar (ArgumentError)
from /Users/matt/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:312:in `resolve_class'
...
This is because Psych needs to create an instance of class Bar, but doesn’t have the definition of the class available. This explains why adding require 'whatever' before loading the Yaml works – now Ruby has the definition of the class loaded and so can create an instance of it (note that there is no definitive link between class name and file name in Ruby, it’s just convention).
The solution therefore is to make sure that when you’re loading any Ruby objects from Yaml you have already required any files that may contain definitions of any classes potentially in that Yaml.

Matt's Answer provides necessary details.
But when I do changes in code and then do some task, that de-serializes some data, without page load (in AJAX) then it fails with same error.
It's better to use require_dependency than require to autoload your changes.
In case of module (not tested with class) and de-serializing using YAML, you can also instantiate your Module before de-serializing with require to fix the issue. See here.
Source: SO answer and Psych issue report in Github
P.S: This problem persists only in Development as config.cache_classes is enable in Production.

Amending to Matt's answer. In lieu of a pure-hash based solution (which the Psych team seems to have no interest in doing), modify the header string to strip off the object classification. I did this simply with the following code.
yamltext = File.read("somefile","r")
yamltext.sub!(/^--- \!.*$/,'---')
hash = YAML.load(yamltext)
This might fail (I don't know) if the input stream consists of several documents with different object classifiers (I dont know if that's even valid).

Related

NoMethodError: Undefined method 'type' for nil:NilClass in Rails

Getting a NoMethodError when trying to load up one of the pages on my website built with Rails. See the full stack trace below.
If I change false to true in production.rb at line
config.cache_classes = false
then the problem goes away, but I have to restart the Rails server every time I make a change to any file. It seems like the problem is related to checking the type of a column in my DB, but I can't figure out how to resolve it.
Controller action:
def manage
#view_data = {}
if (#view_data[:product] = Product.find(params[:id])).nil?
raise ActionController::RoutingError.new('Not Found')
end
#view_data[:backlog] = Task.where("product_id = ? AND status < ?", params[:id], Task::STATUS_COMPLETED)
.page(params[:page]).per(10)
#view_data[:completed] = Task.where(product_id: params[:id], status: Task::STATUS_COMPLETED)
.page(params[:page]).per(10)
end
There are four columns on the 'tasks' table that are type datetime--I think those are causing the problem, but I'm not fully sure.
Full stack trace:
NoMethodError - undefined method `type' for nil:NilClass:
activerecord (3.2.13) lib/active_record/attribute_methods/time_zone_conversion.rb:60:in `create_time_zone_conversion_attribute?'
activerecord (3.2.13) lib/active_record/attribute_methods/time_zone_conversion.rb:24:in `attribute_cast_code'
activerecord (3.2.13) lib/active_record/attribute_methods/serialization.rb:80:in `attribute_cast_code'
activerecord (3.2.13) lib/active_record/attribute_methods/read.rb:72:in `define_method_attribute'
activerecord (3.2.13) lib/active_record/attribute_methods/primary_key.rb:29:in `define_method_attribute'
activemodel (3.2.13) lib/active_model/attribute_methods.rb:267:in `block in define_attribute_method'
activemodel (3.2.13) lib/active_model/attribute_methods.rb:260:in `define_attribute_method'
activemodel (3.2.13) lib/active_model/attribute_methods.rb:256:in `block in define_attribute_methods'
activemodel (3.2.13) lib/active_model/attribute_methods.rb:256:in `define_attribute_methods'
activerecord (3.2.13) lib/active_record/attribute_methods.rb:66:in `block in define_attribute_methods'
activerecord (3.2.13) lib/active_record/attribute_methods.rb:63:in `define_attribute_methods'
activerecord (3.2.13) lib/active_record/attribute_methods.rb:168:in `respond_to?'
activesupport (3.2.13) lib/active_support/callbacks.rb:398:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_initialize_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.13) lib/active_record/base.rb:501:in `initialize'
app/controllers/products_controller.rb:14:in `manage'
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.13) lib/active_support/callbacks.rb:425:in `_run__115665803178931257__process_action__4080704983955383443__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__4157597128006817714__call__2909158845673021585__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
better_errors (0.9.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (0.9.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (0.9.0) lib/better_errors/middleware.rb:56:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
In my case I had a model with 76 attributes, and one of them was named 'class', this was causing the error and exception.
So, I did:
rake/bin db:rollback
rails d scaffold MyModel
rename 'class' column in scaffold generator command
rename 'class' in csv header
rails g scaffold MyModel blablaba..
rake/bin db:migrate
import data
rails s
And works :-)
It seems like your error is related to this bug. There are some proposed solutions that may be relevant for your situation, or you could try this fix by alsemyonov.
Try setting config.autoload_once_paths

undefined method `each' for "":String with update_attributes

So I have an app that pre-register the users. Future user can claim their profile but the admin have to review it manually.
So, I have a table users and user_revision. If the claim is rejected by the admin I need to revert the change made.
Here is how I do it:
Followup.find(params[:followup]).user.update_attributes(UserRevision.find(followup.i_nid).attributes)
But for some reasons suddenly I have the following error, and I have no idea how to fix it:
NoMethodError (undefined method `each' for "":String):
Edit: Here is the full trace
activerecord (3.2.13) lib/active_record/associations/collection_association.rb:310:in `replace'
activerecord (3.2.13) lib/active_record/associations/collection_association.rb:41:in `writer'
activerecord (3.2.13) lib/active_record/associations/builder/association.rb:51:in `block in define_writers'
activerecord (3.2.13) lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
activerecord (3.2.13) lib/active_record/attribute_assignment.rb:78:in `each'
activerecord (3.2.13) lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
activerecord (3.2.13) lib/active_record/persistence.rb:216:in `block in update_attributes'
activerecord (3.2.13) lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.2.13) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.2.13) lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
activerecord (3.2.13) lib/active_record/persistence.rb:215:in `update_attributes'
app/controllers/admin/contractor_controller.rb:66:in `reject'
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.13) lib/active_support/callbacks.rb:458:in `_run__3878365275348389010__process_action__3682866777824238009__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__3678957069864581916__call__81522373500476479__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.1) lib/thin/connection.rb:79:in `catch'
thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.1) lib/thin/connection.rb:54:in `process'
thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
thin (1.5.1) lib/thin/server.rb:159:in `start'
rack (1.4.5) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.5) lib/rack/server.rb:268:in `start'
railties (3.2.13) lib/rails/commands/server.rb:70:in `start'
railties (3.2.13) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.13) lib/rails/commands.rb:50:in `tap'
railties (3.2.13) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'
I helped bl0b with this on the #rubyonrails IRC channel. Turns out that this line from the stacktrace:
activerecord (3.2.13) lib/active_record/associations/collection_association.rb:310:in `replace'
Indicates that AR is attempting to assign to a collection association. I asked OP to go through and attempt to assign each attribute one at a time in the update_attributes call. It turns out it was the services attribute which caused this, because there was also an association called "services" on the record.
Removing the services attribute from the list of attributes to be updated solves this problem.

Ruby on Rails Tutorial: 7.1.3 Could not find table 'users' using mysql

This question relates to switching from sqlite to mysql in rails prior to doing any database operations, while following the Ruby on Rails Tutorial (2nd ed.).
Before I did any database operations with the Ruby on Rails Tutorial (2nd ed.), I switched the database parameters in the config/database.yml to use mysql. Everything has been working up to section 7.1.3. So far the users table was created and a sample user was added to the table successfully. However, when I go to test the show users page at users/1 I receive the error message below. Is there something else that needs to be done when using a mysql database instead of sqlite? Thank you in advance for any support!
Error:
ActiveRecord::StatementInvalid in UsersController#show
Could not find table 'users'
Full Trace:
activerecord (3.2.11) lib/active_record/connection_adapters/sqlite_adapter.rb:472:in `table_structure'
activerecord (3.2.11) lib/active_record/connection_adapters/sqlite_adapter.rb:346:in `columns'
activerecord (3.2.11) lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
activerecord (3.2.11) lib/active_record/model_schema.rb:228:in `yield'
activerecord (3.2.11) lib/active_record/model_schema.rb:228:in `default'
activerecord (3.2.11) lib/active_record/model_schema.rb:228:in `columns'
activerecord (3.2.11) lib/active_record/model_schema.rb:237:in `columns_hash'
activerecord (3.2.11) lib/active_record/relation/delegation.rb:7:in `columns_hash'
activerecord (3.2.11) lib/active_record/relation/finder_methods.rb:331:in `find_one'
activerecord (3.2.11) lib/active_record/relation/finder_methods.rb:312:in `find_with_ids'
activerecord (3.2.11) lib/active_record/relation/finder_methods.rb:107:in `find'
activerecord (3.2.11) lib/active_record/querying.rb:5:in `find'
app/controllers/users_controller.rb:4:in `show'
actionpack (3.2.11) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.11) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.11) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.11) lib/active_support/callbacks.rb:414:in `_run__1973165003296325481__process_action__197160741547540830__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.11) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.11) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.11) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.11) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.11) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.11) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.11) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:601:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.11) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `_run__2423197318852707609__call__2019361553739003282__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.11) lib/rails/engine.rb:479:in `call'
railties (3.2.11) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/opt/bitnami/ruby/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/opt/bitnami/ruby/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/opt/bitnami/ruby/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
From the stacktrace you provided, it looks like your app is not pointing at your mysql database.
activerecord (3.2.11) lib/active_record/connection_adapters/sqlite_adapter.rb:472:intable_structure'` <-- points at the sqlite_adapter rather than a mysql adapter
Make sure you use the mysql2 gem and don't forget to run bundle install after modifying your gemfile. Also be sure to restart your server after modifying your database.yml file.

Rails Paperclip gem wrong number of arguments error

I'm working on my first RoR project and attempting to use the Paperclip gem, but I'm running into an error I can't seem to figure out:
wrong number of arguments (1 for 0)
app/controllers/events_controller.rb:43:in `create'
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"OBYHA0M+TA93sNF3uqrJ/zvnnEyWJUREn4NcOl0ExfA=",
"event"=>{"title"=>"",
"image"=>#<ActionDispatch::Http::UploadedFile:0x007f803802b1f0 #original_filename="icon.png",
#content_type="image/png",
#headers="Content-Disposition: form-data; name=\"event[image]\"; filename=\"icon.png\"\r\nContent-Type: image/png\r\n",
#tempfile=#<File:/tmp/RackMultipart20130217-21480-1molvwa>>,
"description"=>"",
"date"=>"",
"time"=>""},
"commit"=>"Create Event"}
I understand that in events_controller.rb in the create method, too many arguments are being passed in. However, I followed the QuickStart instructions on Paperclip's Github so I'm not sure why I'm getting this error.
Here is my code for events_controller.rb
def create
#event = Event.create( params[:event] )
end
Just for debugging, I tried changing the line to Event.create( params[] ), at which point the error changed from wrong number of arguments (1 for 0) to wrong number of arguments (0 for 1).
Here is my code for event.rb
class Event < ActiveRecord::Base
attr_accessible :date, :description, :time, :title, :image
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
I've checked that I'm running the correct versions for everything. I have Paperclip 3.4.0, Rails 3.2.12, Ruby 1.9.3, and ImageMagick 6.8.3-1. I'm using Ubuntu 12.04 LTS.
I've looked at quite a few similar problems but most seemed to occur a few years ago and were resolved in subsequent updates to Paperclip. None of the solutions I've found in similar posts have worked for me, and unfortunately I just have enough Rails experience yet to know what's going on. I realize I'm probably making a total newbie mistake, in which case I apologize but would still really appreciate some help to get me on the right track.
Please let me know if any additional code or information would be useful for debugging.
Thanks for your time!
Application trace:
cocaine (0.3.2) lib/cocaine/command_line.rb:63:in run'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/helpers.rb:31:in
run'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/geometry_detector_factory.rb:18:in
block in geometry_string' activesupport (3.2.12)
lib/active_support/core_ext/kernel/reporting.rb:43:insilence_stream'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/geometry_detector_factory.rb:17:in
geometry_string'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/geometry_detector_factory.rb:9:in
make'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/geometry.rb:26:in
from_file'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/thumbnail.rb:35:in
initialize'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/processor.rb:33:in
new'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/processor.rb:33:in
make'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/attachment.rb:431:in
block in post_process_style'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/attachment.rb:430:in
each'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/attachment.rb:430:in
inject'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/attachment.rb:430:in
post_process_style'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/attachment.rb:423:in
block in post_process_styles'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/attachment.rb:422:in
each'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/attachment.rb:422:in
post_process_styles'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/attachment.rb:415:in
block (2 levels) in post_process' activesupport (3.2.12)
lib/active_support/callbacks.rb:403:in
_run__3384881169595487878__image_post_process__96086910066492375__callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in
__run_callback' activesupport (3.2.12)
lib/active_support/callbacks.rb:385:in
_run_image_post_process_callbacks' activesupport (3.2.12)
lib/active_support/callbacks.rb:81:inrun_callbacks'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/callbacks.rb:26:in
run_paperclip_callbacks'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/attachment.rb:414:in
block in post_process' activesupport (3.2.12)
lib/active_support/callbacks.rb:403:in
_run__3384881169595487878__post_process__96086910066492375__callbacks' activesupport (3.2.12) lib/active_support/callbacks.rb:405:in
__run_callback' activesupport (3.2.12)
lib/active_support/callbacks.rb:385:in _run_post_process_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in
run_callbacks'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/callbacks.rb:26:in
run_paperclip_callbacks'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/attachment.rb:413:in
post_process'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip/attachment.rb:108:in
assign'
/home/user/.rvm/gems/ruby-1.9.3-p385/bundler/gems/paperclip-dcd2774e7913/lib/paperclip.rb:200:inblock in has_attached_file' activerecord (3.2.12)
lib/active_record/attribute_assignment.rb:85:in block in
assign_attributes' activerecord (3.2.12)
lib/active_record/attribute_assignment.rb:78:ineach' activerecord
(3.2.12) lib/active_record/attribute_assignment.rb:78:in
assign_attributes' activerecord (3.2.12)
lib/active_record/base.rb:497:ininitialize' activerecord (3.2.12)
lib/active_record/persistence.rb:44:in new' activerecord (3.2.12)
lib/active_record/persistence.rb:44:increate'
app/controllers/events_controller.rb:43:in create' actionpack
(3.2.12) lib/action_controller/metal/implicit_render.rb:4:in
send_action' actionpack (3.2.12)
lib/abstract_controller/base.rb:167:in process_action' actionpack
(3.2.12) lib/action_controller/metal/rendering.rb:10:in
process_action' actionpack (3.2.12)
lib/abstract_controller/callbacks.rb:18:in block in process_action'
activesupport (3.2.12) lib/active_support/callbacks.rb:414:in
run_3332570171411252513__process_action_3320642634939127128_callbacks' activesupport (3.2.12) lib/active_support/callbacks.rb:405:in
__run_callback' activesupport (3.2.12)
lib/active_support/callbacks.rb:385:in_run_process_action_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in
run_callbacks' actionpack (3.2.12)
lib/abstract_controller/callbacks.rb:17:inprocess_action' actionpack
(3.2.12) lib/action_controller/metal/rescue.rb:29:in process_action'
actionpack (3.2.12)
lib/action_controller/metal/instrumentation.rb:30:inblock in
process_action' activesupport (3.2.12)
lib/active_support/notifications.rb:123:in block in instrument'
activesupport (3.2.12)
lib/active_support/notifications/instrumenter.rb:20:ininstrument'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in
instrument' actionpack (3.2.12)
lib/action_controller/metal/instrumentation.rb:29:inprocess_action'
actionpack (3.2.12)
lib/action_controller/metal/params_wrapper.rb:207:in process_action'
activerecord (3.2.12)
lib/active_record/railties/controller_runtime.rb:18:in
process_action' actionpack (3.2.12)
lib/abstract_controller/base.rb:121:in process' actionpack (3.2.12)
lib/abstract_controller/rendering.rb:45:inprocess' actionpack
(3.2.12) lib/action_controller/metal.rb:203:in dispatch' actionpack
(3.2.12) lib/action_controller/metal/rack_delegation.rb:14:in
dispatch' actionpack (3.2.12) lib/action_controller/metal.rb:246:in
block in action' actionpack (3.2.12)
lib/action_dispatch/routing/route_set.rb:73:incall' actionpack
(3.2.12) lib/action_dispatch/routing/route_set.rb:73:in dispatch'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:36:in
call' journey (1.0.4) lib/journey/router.rb:68:in block in call'
journey (1.0.4) lib/journey/router.rb:56:ineach' journey (1.0.4)
lib/journey/router.rb:56:in call' actionpack (3.2.12)
lib/action_dispatch/routing/route_set.rb:601:incall' actionpack
(3.2.12)
lib/action_dispatch/middleware/best_standards_support.rb:17:in call'
rack (1.4.5) lib/rack/etag.rb:23:incall' rack (1.4.5)
lib/rack/conditionalget.rb:35:in call' actionpack (3.2.12)
lib/action_dispatch/middleware/head.rb:14:incall' actionpack
(3.2.12) lib/action_dispatch/middleware/params_parser.rb:21:in call'
actionpack (3.2.12) lib/action_dispatch/middleware/flash.rb:242:in
call' rack (1.4.5) lib/rack/session/abstract/id.rb:210:in context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:incall' actionpack
(3.2.12) lib/action_dispatch/middleware/cookies.rb:341:in call'
activerecord (3.2.12) lib/active_record/query_cache.rb:64:incall'
activerecord (3.2.12)
lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in
call' actionpack (3.2.12)
lib/action_dispatch/middleware/callbacks.rb:28:inblock in call'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in
_run__1281245379307539657__call__96086910066492375__callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in
__run_callback' activesupport (3.2.12)
lib/active_support/callbacks.rb:385:in _run_call_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in
run_callbacks' actionpack (3.2.12)
lib/action_dispatch/middleware/callbacks.rb:27:in call' actionpack
(3.2.12) lib/action_dispatch/middleware/reloader.rb:65:incall'
actionpack (3.2.12) lib/action_dispatch/middleware/remote_ip.rb:31:in
call' actionpack (3.2.12)
lib/action_dispatch/middleware/debug_exceptions.rb:16:incall'
actionpack (3.2.12)
lib/action_dispatch/middleware/show_exceptions.rb:56:in call'
railties (3.2.12) lib/rails/rack/logger.rb:32:incall_app' railties
(3.2.12) lib/rails/rack/logger.rb:16:in block in call' activesupport
(3.2.12) lib/active_support/tagged_logging.rb:22:intagged' railties
(3.2.12) lib/rails/rack/logger.rb:16:in call' actionpack (3.2.12)
lib/action_dispatch/middleware/request_id.rb:22:incall' rack (1.4.5)
lib/rack/methodoverride.rb:21:in call' rack (1.4.5)
lib/rack/runtime.rb:17:incall' activesupport (3.2.12)
lib/active_support/cache/strategy/local_cache.rb:72:in call' rack
(1.4.5) lib/rack/lock.rb:15:incall' actionpack (3.2.12)
lib/action_dispatch/middleware/static.rb:62:in call' railties
(3.2.12) lib/rails/engine.rb:479:incall' railties (3.2.12)
lib/rails/application.rb:223:in call' rack (1.4.5)
lib/rack/content_length.rb:14:incall' railties (3.2.12)
lib/rails/rack/log_tailer.rb:17:in call' rack (1.4.5)
lib/rack/handler/webrick.rb:59:inservice'
/home/user/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/webrick/httpserver.rb:138:in
service'
/home/user/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/webrick/httpserver.rb:94:in
run'
/home/user/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/webrick/server.rb:191:in
`block in start_thread'
You are using the wrong version of cocaine. Run bundle install then make sure you are using bundle exec rails s to start your rails server.
The run method in the newer version of cocaine (~> 0.4.0) takes an optional argument (def run(interpolations = {})) that the older version (0.3.2) didn't.
See comments above.

Ruby on Rails - Can not get associations work

Another nooby question - I have these two models in my project:
class Invoice < ActiveRecord::Base
has_many :items
end
and
class Item < ActiveRecord::Base
belongs_to :invoice ,:foreign_key=>'invoice_number',:primary_key=>'invoice_number'
end
So in my controller, I want to add new item to the invoice by pressing a button which is binded to an action. The action will generate a form to let the client fill in the information about the item. I want to something like this in controller:
#invoice.items << Item.new
This operation might be wrong ,but the problem is , Rails tells me that there is undefine method 'items'. Does anybody could give me a hint on how to fix this?
Here is the error trace:
activemodel (3.2.8) lib/active_model/attribute_methods.rb:407:in `method_missing'
activerecord (3.2.8) lib/active_record/attribute_methods.rb:149:in `method_missing'
app/controllers/return_controller.rb:4:in `index'
actionpack (3.2.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.8) lib/active_support/callbacks.rb:414:in `_run__3004877794972512718__process_action__435319474484567757__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.8) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.8) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:600:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/cookies.rb:339:in `call'
activerecord (3.2.8) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `_run__2876506060705866238__call__963306619056181882__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.8) lib/rails/engine.rb:479:in `call'
railties (3.2.8) lib/rails/application.rb:223:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
and in console it said:
NoMethodError (undefined method `items' for #<Invoice:0x00000002b822c8>):
app/controllers/invoice_controller.rb:4:in `index'
Feedback for the Question:
Thank you for you guys helping me on this mass.I have solve the problem - I found a misspelling (missing 's'), and I have sorry for taking your time.
Thanks again
Did you create these models in your database and give Item an invoice_number?
rails g migration AddInvoiceNumberToItems invoice_number:integer
rake db:migrate

Resources