has_many_polymorphs icon indicating copy to clipboard operation
has_many_polymorphs copied to clipboard

Impossible to create an association with no polymorphic side.

Open rurounijones opened this issue 16 years ago • 0 comments

I am using ACL ( http://github.com/rurounijones/acl9/tree/master ) for access control and combining it with has_many_polymorphs for easy associations.

It is working great with one problem. ACL9 allows you to assign roles on objects,Classes or nothing. When I assign a role on an Object or Class then everything works fine (authorizable_type is not nil in these cases). However when I add a global role ( where authorizable_type is nil) I get the following error.

  The error occurred while evaluating nil.constantize
    from (eval):7:in `before_save'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:347:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:347:in `callback'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:249:in `create_or_update'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2539:in `save_without_validation'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:1009:in `save_without_dirty'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/dirty.rb:79:in `save_without_transactions'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:229:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:229:in `with_transaction_returning_status'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:182:in `transaction'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:228:in `with_transaction_returning_status'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:196:in `save'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:196:in `save'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:242:in `create'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:410:in `create_record'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:428:in `add_record_to_target_with_callbacks'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:410:in `create_record'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:240:in `create'

I believe the error lies in this line:

def association_class
  @owner[@reflection.options[:foreign_type]] ? @owner[@reflection.options[:foreign_type]].constantize : nil
end

Is it possible to change this line to allow for associations where the polymorphic side is nil? In effect ignoring the :through aspect of the association record if the polymorphic side is nil and just leave it to the normal has_many.

Regards

Jeff Jones

Classes follow:

class User < ActiveRecord::Base
  has_many :roles
  has_many_polymorphs :authorizables, :from => [:committees], :through => :roles
end

class Role < ActiveRecord::Base
    belongs_to :user
    belongs_to :authorizable, :polymorphic => true
end

class Committee < ActiveRecord::Base
    has_many :accepted_roles, :as => :authorizable, :class_name => :role, :dependent => :destroy
end

rurounijones avatar Aug 23 '09 02:08 rurounijones