Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclass misses association defined on parent #20678

Open
matthewd opened this issue Jun 23, 2015 · 7 comments · May be fixed by #39473
Open

Subclass misses association defined on parent #20678

matthewd opened this issue Jun 23, 2015 · 7 comments · May be fixed by #39473

Comments

@matthewd
Copy link
Member

If an association is defined on the superclass after a subclass has been given an association of its own, the subclass will have the association methods present, but calling them will raise AssociationNotFoundError.

https://gist.github.com/matthewd/8b88b7356d4076fe6a10

@thedarkone
Copy link
Contributor

To anyone that is going to work on this, I think this is very similar to #18926.

@akihiro17
Copy link
Contributor

I guess the cause of this issue would be that
Association#associationmethod tries to get the reflection only in FancyPost._reflections.

Post._reflections['comments'] =>
#<ActiveRecord::Reflection::HasManyReflection:0x007fb24bb55b08 @name=:comments, @scope=nil, @options={}, @active_record=Post(id: integer), @klass=nil, @plural_name="comments", @automatic_inverse_of=nil, @type=nil, @foreign_type="comments_type", @constructable=true, @association_scope_cache={}, @scope_lock=#<Mutex:0x007fb24bb7df18>>

FancyPost._reflections['comments'] =>
nil

My workaround for this issue is shown below.

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations.rb#L153

def association(name) #:nodoc:
  association = association_instance_get(name)

  if association.nil?
    reflection = self.class._reflect_on_association(name)

    # if reflection is nil, try to get the parent class' reflection
    klass = self.class.superclass
    while reflection.nil? && klass.respond_to?(:_reflect_on_association)
      reflection = klass._reflect_on_association(name)
      klass = klass.superclass
    end

    unless reflection
      raise AssociationNotFoundError.new(self, name)
    end
    association = reflection.association_class.new(self, reflection)
    association_instance_set(name, association)
  end
  association
end

@rails-bot
Copy link

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-2-stable, 4-1-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@arthurnn arthurnn added pinned and removed stale labels Jan 27, 2016
@dguettler
Copy link
Contributor

Just ran into this issue when trying to delete a record which is a subclass. Above workaround mitigates the issue.
ActiveRecord: 4.2.5.1

# Good
class A < ActiveRecord::Base
end

class B < A
end

class A
  has_many :apples
end

B._reflections["apples"] => Reflection

# Bad
class A < ActiveRecord::Base
end

class B < A
  has_many :oranges
end

class A
  has_many :apples
end

B._reflections["apples"] => nil

@maclover7
Copy link
Contributor

Opened #24719.

@joenio
Copy link

joenio commented May 11, 2016

related to #6944

marcosronaldo added a commit to marcosronaldo/noosfero that referenced this issue May 12, 2016
Temporary fix, waiting for original fix to be merged.

Original author: Jon Moss
Merge Request: rails/rails#24719
Issue: rails/rails#20678

Signed-off-by: Joenio Costa <joenio@colivre.coop.br>
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
@brauliobo
Copy link

related to #21908

maclover7 added a commit to maclover7/rails that referenced this issue May 16, 2016
- Fixes rails#20678 (Subclass misses association defined on parent)

[Jon Moss, Bráulio Bhavamitra]
vfcosta pushed a commit to vfcosta/noosfero that referenced this issue May 17, 2016
Temporary fix, waiting for original fix to be merged.

Original author: Jon Moss
Merge Request: rails/rails#24719
Issue: rails/rails#20678

Signed-off-by: Joenio Costa <joenio@colivre.coop.br>
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
vfcosta pushed a commit to vfcosta/noosfero that referenced this issue May 17, 2016
Fixes ActiveRecord relations for descendants

Temporary fix, waiting for original fix to be merged.

Original author: Jon Moss
Merge Request: rails/rails#24719
Issue: rails/rails#20678

See merge request !914
tallysmartins pushed a commit to tallysmartins/noosfero that referenced this issue May 18, 2016
Temporary fix, waiting for original fix to be merged.

Original author: Jon Moss
Merge Request: rails/rails#24719
Issue: rails/rails#20678

Signed-off-by: Joenio Costa <joenio@colivre.coop.br>
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
(cherry picked from commit 3b60e7c)
tallysmartins pushed a commit to tallysmartins/noosfero that referenced this issue May 18, 2016
Temporary fix, waiting for original fix to be merged.

Original author: Jon Moss
Merge Request: rails/rails#24719
Issue: rails/rails#20678

Signed-off-by: Joenio Costa <joenio@colivre.coop.br>
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
kamipo added a commit to kamipo/rails that referenced this issue May 29, 2020
This is a common `class_attribute` implementation issue (e.g. rails#39467,
rails#39468, rails#38871).

For now, we should take care of the change propagation by themselves if
we want to ensure the propagation after the `class_attribute` are
accessed in subclasses.

Fixes rails#20678.
liaham added a commit to xmera-circle/redmine_project_types_relations that referenced this issue May 2, 2023
The problem was 'Association named 'project_custom_fields' was not found on ProjectType'.

This error occurs although the parent class of ProjectType, what is Project,
has the association above defined.

This problem is related to Rails rails/rails#20678 (comment).

Implements #1474
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants