Skip to content

Error when saving an empty polymorphic association with Mongoid and ActiveRecord #49

@rotev

Description

@rotev

Hi there,

I'm having difficulties saving an object with a polymorphic association that's empty.
This is my code:

class Object < ActiveRecord::Base
  include Tenacity
  t_has_one :tag, as: :taggable
end
class Tag
  include Mongoid::Document
  include Tenacity
  t_belongs_to :taggable, polymorphic: true
end
t = Tag.new
t.valid? # NoMethodError: undefined method `empty?' for nil:NilClass
# from /home/.../gems/tenacity-0.6.0/lib/tenacity/association.rb:115:in `qualified_const_get'
t.save # Same error

But if I add a taggable object to it, it works as expected:

t = Tag.new
t.taggable = Object.new
t.valid? # true
t.save # true

To solve this I used Rails' blank? method in several places:

  • in lib/tenacity/instance_methods.rb:21 I changed unless associate_id.nil? to unless associate_id.blank?
  • in lib/tenacity/association.rb:114 I changed return nil if clazz.nil? to return nil if clazz.blank?
  • and in lib/tenacity/associations/belongs_to.rb:21 I changed clazz._t_find(associate_id) to clazz._t_find(associate_id) unless clazz.nil?.

Rethinking it, using blank? isn't good, because this gem should work without Rails. I also tried to run the tests but I had difficulties installing the rcov gem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions