#1003 new
ronin-25958 (at lighthouseapp)

Polymorphic associations broken in hobo 1.4.0.pre5

Reported by ronin-25958 (at lighthouseapp) | April 1st, 2012 @ 02:05 AM

In hobo 1.4.0.pre5 a polymorphic association similar to:

class Product < ActiveRecord::Base
  has_many :adaptions, :as => :adaptable, :dependent => :destroy
end

class ItemAdaption < ActiveRecord::Base
  belongs_to :adaptable, :polymorphic => true
end

raises:
...gems/hobo/lib/hobo/model.rb:188:in `class_eval': (eval):3: syntax error, unexpected tANDOP (SyntaxError) ... target.class.name == self. && target.id == self.adaptable_...

Comments and changes to this ticket

  • ronin-25958 (at lighthouseapp)

    ronin-25958 (at lighthouseapp) April 1st, 2012 @ 11:58 AM

    Not a complete April Fools' joke, but the line number given in the error message was a lie (as I had modified the file to trace the error).
    Here's how to reproduce it with a fresh 1.4.0.pre4 installation (Ruby 1.9.2 or 1.9.3):

    In the User model add:

     has_many :activities, :as => :target
    

    Create a new Activity model:

    class Activity < ActiveRecord::Base
     hobo_model
     belongs_to :target, :polymorphic => true
    end
    

    hobo g migration

    /Users/macbook/.rvm/gems/ruby-1.9.2-p318/gems/hobo-1.4.0.pre4/lib/hobo/model.rb:175:in `class_eval': (eval):3: syntax error, unexpected tANDOP (SyntaxError) ... target.class.name == self. && target.id == self.target_id

    refl.options[:foreign_type] seems to be nil...

  • Karsten Redmer

    Karsten Redmer May 15th, 2012 @ 07:40 PM

    Error also in 1.4.0.pre6

    Solution:

    Replace around line number 170:

    refl.options[:foreign_type]

    with

    refl.foreign_key

       class_eval %{
            def #{name}_is?(target)
              target.class.name == self.#{refl.foreign_type} && target.#{id_method} == self.#{refl.foreign_key}
            end
            def #{name}_changed?
              #{refl.foreign_key}_changed? || #{refl.foreign_type}_changed?
            end
          }
    

    I think no pull request is needed......but would be nice to see this change in 1.4.0.pre7

  • Karsten Redmer

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

People watching this ticket

Pages