jwood / tenacity

A database client independent way of managing relationships between models backed by different databases.
MIT License
118 stars 17 forks source link

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

Open rotev opened 10 years ago

rotev commented 10 years ago

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:

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.

jwood commented 10 years ago

Hi!

As you can probably tell by the commit history, tenacity hasn't been under active development in quite some time, and I'm not actively supporting it. However, I do try to help address bugs that come in.

I tried installing the dependencies with Ruby 2.0.0, and ran into the same rcov problem that you did. I switched to ruby 1.9.3p484, and was able to install all of the dependencies just fine, and run the tests (via bundle exec rake).

With regards to blank?, tenacity does currently have a runtime dependency on ActiveSupport, which is where blank? is defined. So, it is safe to use blank? in tenacity code.

If you are able to submit a pull request with your changes, a new test for this error case, and all existing tests pass, I would be more than happy to merge it in and cut a new version of gem.

Sorry I can't be or more assistance. I'm simply swamped at the moment.