Ruby NoMethodError: undefined method `messages' for []:Array - ruby-on-rails

Since google did not help me in my quest to figure this out, I figured I would share it for anyone else that runs into this problem:
When attempting to save a model, I get
NoMethodError: undefined method `messages' for []:Array
0: /.../active_record/autosave_association.rb:491:in `_ensure_no_duplicate_errors'
1: /.../active_support/callbacks.rb:413:in `block in make_lambda'
2: /.../active_support/callbacks.rb:246:in `block in halting'
3: /.../active_support/callbacks.rb:511:in `block in invoke_after'
4: /.../active_support/callbacks.rb:511:in `each'
5: /.../active_support/callbacks.rb:511:in `invoke_after'
6: /.../active_support/callbacks.rb:132:in `run_callbacks'
7: /.../active_support/callbacks.rb:825:in `_run_validation_callbacks'
8: /.../active_model/validations/callbacks.rb:110:in `run_validations!'
9: /.../active_model/validations.rb:335:in `valid?'
Example model:
class Foo < ActiveRecord::Base
def do_stuff
#errors = []
#errors << 'Bad stuff' if self.bar > 4
#errors
end
end
Example code:
foo = Foo.first
foo.do_stuff
foo.save # or foo.valid?, etc.

The Cause
The cause of this error message is that the Foo class creates an instance level #errors variable.
ActiveRecord keeps track of database errors in a model's errors variable. When you try to validate a model, it checks for error messages in this variable.
The Solution
Change #errors to a different name (e.g. #foo_errors).

Related

o.respond_to?(:empty?) and o.empty? Fails with: "undefined method `empty?`"

so how is that possible?
I have a Module, that puts a Ruby Object at the end of the renderd page in a nice structured HTML. So i recurse through the given object and build the HTML output. The following is an excerpt of the code where the error is thrown.
EDIT:(had a copy error in code)
o=some object
nicer=if o.respond_to?(:empty?) and o.empty?
add_class='empty'
'empty ' + class_name
else
case o
when TrueClass then
"TRUE"
when FalseClass then
"FALSE"
when Array
#some more when's
the error thrown: undefined method 'empty?' for #Journey::Routes:0x123456
the object (o) it self is ActionDispatch::Routing::RouteSet
again: how is that possible?
EDIT: Stack: (there is the bad one ...)
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:366:in `empty?'
lib/tech_draw.rb:90:in `format_nice'
lib/tech_draw.rb:101:in `block in format_nice'
lib/tech_draw.rb:100:in `each'
lib/tech_draw.rb:100:in `map'
lib/tech_draw.rb:100:in `format_nice'
lib/tech_draw.rb:124:in `block in format_nice'
lib/tech_draw.rb:123:in `map'
lib/tech_draw.rb:123:in `format_nice'
lib/tech_draw.rb:13:in `block in say'
lib/tech_draw.rb:13:in `map'
lib/tech_draw.rb:13:in `say'
lib/tech_draw.rb:13:in `map'
lib/tech_draw.rb:6:in `say'
app/controllers/home_controller.rb:131:in `any_page'
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
...
How is that possible? Easy:
class Thing
def respond_to? *args
true
end
end
o = Thing.new
o.respond_to?(:empty?) and o.empty?
# => NoMethodError: undefined method `empty?' for #<Thing:0x00000100ae2558>
Although why is it happening in this case is another matter.
ActionDispatch::Routing::RouteSet#empty? appears to call empty? on routes object. Assuming this object is an instance of Journey::Routes that would explain the error, as Journey::Routes doesn’t have an empty? method. (In current Rails versions Journey is part of Rails itself, but in Rails 3.2 it is separate).
I don’t why this is happening in your case though.

undefined method `yaml' for nil:NilClass when trying to save duplicated object

I have an Active Record model object called Event, which has many event_things. I want to be able to duplicate the event, so that it gets a new id. I'm using Rails 3.2 and in rails console am able to successfully call
event_copy = event.dup
event_copy.save
However, I also want to duplicate each of the Event's event_things.
copy = event_thing.dup
copy.event_id = event_copy.id
copy.save
But that gives me this error stack:
NoMethodError: undefined method `yaml' for nil:NilClass
from /Users/Ed/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/psych.rb:204:in `dump_stream'
from /Users/Ed/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/psych/core_ext.rb:35:in `psych_y'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.6/lib/active_model/dirty.rb:143:in `attribute_change'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.6/lib/active_model/dirty.rb:117:in `block in changes'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.6/lib/active_model/dirty.rb:117:in `map'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.6/lib/active_model/dirty.rb:117:in `changes'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/attribute_methods/dirty.rb:23:in `save'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:208:in `transaction'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:241:in `block in save'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:240:in `save'
from (irb):18
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
I ran into the same issue as this. It turns out that there's a Kernel.y extension method that is getting in your way, based on this article. Here is what I did to fix my problem:
class Event < ActiveRecord::Base
# alias the attributes
alias_attribute :x_pos, :x
alias_attribute :y_pos, :y
after_initialize :init
# after_initialize will still work, with the aliases
def init
self.x_pos ||= 0
self.y_pos ||= 0
end
# override Kernel.y
def y; end
# override self.y_pos to read out of the :attributes, since we blew away :y
def y_pos
attribute['y']
end
end
After doing this, I was finally able to use my model without having to rename the y database column.
While I normally don't advocate using gems liberally, if you have a lot of relations that need to be preserved in the duplication, or find yourself doing these manual duplications a lot, take a look at deep_clonable.
As for your specific error, try surrounding each line of code with a nil check (one line at a time). I'm sure there's just some case where you're ending up with a nil object that you're overlooking.

Undefined method 'detect' for :"Model#property":Symbol. Bad self-referential Neo4j model definitions?

When running unit tests I get the following error for some of my tests:
6) Error:
test_name_can't_be_nil(NodeTest):
NoMethodError: undefined method `detect' for :"Node#children":Symbol
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/naming.rb:85:in `model_name'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:315:in `generate_message'
org/jruby/RubyArray.java:2339:in `collect'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:314:in `generate_message'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:235:in `add'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:256:in `add_on_blank'
org/jruby/RubyArray.java:1615:in `each'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:254:in `add_on_blank'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/validations/presence.rb:9:in `validate'
org/jruby/RubyBasicObject.java:1704:in `__send__'
org/jruby/RubyKernel.java:2101:in `send'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.1.4/lib/active_support/callbacks.rb:306:in `_callback_before_47'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.1.4/lib/active_support/callbacks.rb:410:in `_run_validate_callbacks'
org/jruby/RubyBasicObject.java:1698:in `__send__'
org/jruby/RubyKernel.java:2097:in `send'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.1.4/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/validations.rb:212:in `run_validations!'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/validations.rb:179:in `valid?'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/neo4j-2.0.1-java/lib/neo4j/rails/validations.rb:29:in `valid?'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/neo4j-2.0.1-java/lib/neo4j/rails/callbacks.rb:26:in `valid_with_callbacks?'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.1.4/lib/active_support/callbacks.rb:405:in `_run_validation_callbacks'
/Users/dave/.rvm/gems/jruby-1.6.7.2/gems/neo4j-2.0.1-java/lib/neo4j/rails/callbacks.rb:26:in `valid_with_callbacks?'
/Users/dave/Projects/myapp/test/test_helper.rb:11:in `assert_presence'
/Users/dave/Projects/myapp/test/unit/node_test.rb:9:in `test_name_can't_be_nil'
org/jruby/RubyBasicObject.java:1698:in `__send__'
The offending test in question is:
class NodeTest < ActiveSupport::TestCase
def assert_presence( model, field)
model.send( (field.to_s + '='), nil )
model.valid?
assert_match /can not be blank/, model.errors[field].join,
"Presence error for #{field} not found on #model.class"
end
def setup
#node = FactoryGirl.build :node
end
test "name can't be nil" do
assert_presence #node, :name
end
....
end
Where Nodes are defined in the model as:
class Node < Neo4j::Rails::Model
include Neo4j::NodeMixin
property :name, type: String, index: :exact
property :description, type: String
has_one(:creator).from(User, :created_nodes)
has_n(:children).to(Node) ## <--- Suspected difficult line
has_n(:parents).from(Node, :children) ## <--- Suspected difficult line
has_n(:capable_users).from(User, :capabilities)
# The following has the Class commented out because this somehow triggers
# an error from the 'has_n(:children).to(Node)' line
has_n(:challenges) #.to(Challenge)
....
end
Basically, the server can run fine, but will crash anytime I try to call valid? on a Node (not other models). Does anyone know why this might be happening?
The problem was my choice of attributes, namely parents (and maybe children).
I took a look at the source in active model where the error was occurring (here), and saw this:
def model_name
#_model_name ||= begin
namespace = self.parents.detect { |n| n.respond_to?(:_railtie) } # <--- this line
ActiveModel::Name.new(self, namespace)
end
end
As you can see, parents appears to be in use. I changed the names and the validations, saving, etc, now works.

delayed_job_mongoid cannot use handle_asynchronously but can use .delay.method

I am using Mongoid and delayed_job_mongoid gems. Everything is in its latest version, including Rails (3.2.3).
I understand that if I put handle_asynchronously after any method declaration, this method will always be run async. But I get undefined method error like this:
class Item
# mongoid include stuff
def shout
puts 'a simple hi'
end
handle_asynchronously :shout
end
item = Item.create
item.shout # returns a delayed job object
j = _
# wait a moment
j.last_error
{undefined method `shout_without_delay' for #<Item:0x007f8365e62978>
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-2.4.9/lib/mongoid/attributes.rb:166:in `method_missing'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/performable_method.rb:26:in `perform'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/backend/base.rb:94:in `block in invoke_job'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/lifecycle.rb:60:in `call'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/lifecycle.rb:60:in `block in initialize'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/lifecycle.rb:65:in `call'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/lifecycle.rb:65:in `execute'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/lifecycle.rb:38:in `run_callbacks'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/backend/base.rb:91:in `invoke_job'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/worker.rb:178:in `block (2 levels) in run'
/Users/nik/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/worker.rb:178:in `block in run'
/Users/nik/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
BUT if I had called
item.delay.shout
without the handle_asynchronously line
it will work!
Any ideas?
THANKS!
this is the author of the post. I found out what happened. Short answer: You have to restart the workers in the terminal
[control + c]
rake jobs:work
for mongoid delay jobs to be able to find the method. I do not know why. So just remember to restart the workers whenever you have added another line of handle_asynchronously :method
Happy coding!
You syntax should be
class Item
# mongoid include stuff
def shout
puts 'a simple hi'
end
handle_asynchronously :shout
end
you can get more details from here https://github.com/collectiveidea/delayed_job

using end as column name

I'm maintaining a rails 2.1 application that has some unfortunate choices for column names. For instance, an Event has a start and an end date. Instead of using start_at and end_at the original design uses start and end. Of course this leads to
def end
read_attribute(:end) || 1.hour.from_now
end
I'm surprised this even parses. Is this legal ruby? The real issue is that erb is blowing up with 'stack level too deep' when running a backgroundrb job to send the reminder emails. The template is
<%= [#event.name, #event.when_pretty, #event.location, #event.association,
#event.notes].reject(&:blank?) * "\n" %>
If I deliver_reminder in the console there is no error, but when deliver_reminder is called during the background job, the error occurs.
Question: should I refactor to remove the end method, or is the stack error being caused by something else?
On line #1 of foo/mailer/reminder.rhtml
1: <%= [#event.name, #event.when_pretty, #event.location, #event.association, #event.notes].reject(&:blank?) * "\n" %>
lib/virtual_attributes_and_associations.rb:59:in `virtual_attribute_names'
lib/virtual_attributes_and_associations.rb:83:in `read_attribute_without_virtual'
lib/virtual_attributes_and_associations.rb:86:in `read_attribute'
vendor/rails/activerecord/lib/active_record/base.rb:2720:in `send'
vendor/rails/activerecord/lib/active_record/base.rb:2720:in `clone_attribute_value'
vendor/rails/activerecord/lib/active_record/dirty.rb:127:in `write_attribute'
vendor/rails/activerecord/lib/active_record/attribute_methods.rb:211:in `data='
lib/virtual_attributes_and_associations.rb:9:in `included'
vendor/rails/activesupport/lib/active_support/callbacks.rb:177:in `call'
vendor/rails/activesupport/lib/active_support/callbacks.rb:177:in `evaluate_method'
vendor/rails/activesupport/lib/active_support/callbacks.rb:161:in `call'
vendor/rails/activesupport/lib/active_support/callbacks.rb:93:in `run'
vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `each'
vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `send'
vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `run'
vendor/rails/activesupport/lib/active_support/callbacks.rb:272:in `run_callbacks'
vendor/rails/activerecord/lib/active_record/callbacks.rb:298:in `callback'
vendor/rails/activerecord/lib/active_record/base.rb:1450:in `send'
vendor/rails/activerecord/lib/active_record/base.rb:1450:in `instantiate'
vendor/rails/activerecord/lib/active_record/base.rb:582:in `find_by_sql'
vendor/rails/activerecord/lib/active_record/base.rb:582:in `collect!'
vendor/rails/activerecord/lib/active_record/base.rb:582:in `find_by_sql'
vendor/rails/activerecord/lib/active_record/base.rb:1341:in `find_every'
vendor/rails/activerecord/lib/active_record/base.rb:1376:in `find_one'
vendor/rails/activerecord/lib/active_record/base.rb:1362:in `find_from_ids'
vendor/rails/activerecord/lib/active_record/base.rb:537:in `find'
vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb:44:in `find_target'
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:196:in `load_target'
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:99:in `reload'
vendor/rails/activerecord/lib/active_record/associations.rb:1084:in `contact'
lib/association.rb:52:in `associated_object'
lib/association.rb:48:in `association'
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:177:in `send'
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:177:in `method_missing'
app/views/foo/mailer/reminder.rhtml:1:in `_run_erb_47app47views47foo47mailer47reminder46rhtml'
Turns out the issue was with a library method called association, when I changed that to call associated_object (which was called by Association#association), the stack error no longer occurred.

Resources