Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

STI broken: Association named 'yyy' was not found on Xxx; perhaps you misspelled it? #11939

Closed
greyblake opened this Issue · 12 comments

7 participants

@greyblake

Environment

  • Only Rails3.2.14
  • Ruby1.9,3; 2.0.0

How to reproduce

# Create rails app
rails new sti_bug
cd sti_bug

# Create models
rails g model user 
rails g model user_account user_id:integer
rails g model thing user_account_id:integer type:string

# Run migrations
rake db:migrate
class User < ActiveRecord::Base
  has_many :user_accounts
end

class UserAccount < ActiveRecord::Base
  belongs_to :user
  has_many :things
end

class Thing < ActiveRecord::Base
  belongs_to :user_account

  scope :of_first_user, lambda {
    joins(:user_account => :user).
    where('users.id' => 1)
  }
end

class ConcreteThing < Thing
end

In console:

Thing.of_first_user # => works fine

ConcreteThing.of_first_user # => raise exception
# ActiveRecord::ConfigurationError: Association named 'user' was not found on ConcreteThing; perhaps you misspelled it?

In rails 3.2.13 it works OK.

@joecannatti

Confirmed. Also,

ConcreteThing.joins(:user_account => :user).where('users.id' => 1)

works fine

@rafaelfranca

Thank you for reporting guys. Added to 3.2.15 milestone

@benolee

It appears to have been introduced at c09829e

@benolee

I have a feeling that the class comparison here https://github.com/rails/rails/blob/c09829e03db611b46bb52e2054991222cf57bfbe/activerecord/lib/active_record/relation/spawn_methods.rb#L153 should be something like if other.klass >= relation.klass. Making that change seems to have fixed this bug, but I'm not confident that the change is correct.

@benolee

After some study of AR joins, I think if other.table.name == relation.table.name is correct. I'm not sure if I have to worry about table aliases, though.

@Ben-M Ben-M referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@Ben-M

The Pull Request has been sitting there with no comments for quite a while now. Any hints for getting it reviewed?

@greyblake

Thanks @Ben-M! I hope somebody will take a look at it.

@sikachu sikachu referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@sikachu sikachu referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@sikachu sikachu referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@sikachu sikachu referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@sikachu sikachu referenced this issue from a commit in sikachu/rails
@sikachu sikachu Port test from cf1904f to avoid future regression
Related issue: #11939, #12084
0a26530
@sikachu sikachu referenced this issue from a commit in sikachu/rails
@sikachu sikachu Port test from cf1904f to avoid future regression
Related issue: #11939, #12084
68421f3
@sikachu
Collaborator

Closing this since #12084 got merged.

@sikachu sikachu closed this
@greyblake

Nice! Thanks.

@sikachu
Collaborator

@greyblake would you mind testing out the RC and gives us a thumbs up/down? ;)

@greyblake

@sikachu I've tried 3-2-stable branch, it works fine! Thanks.

@tenderlove tenderlove referenced this issue from a commit
@tenderlove tenderlove Merge branch '3-2-15' into 3-2-sec
* 3-2-15:
  bumping to rc3
  Revert "Merge pull request #12413 from arthurnn/inverse_of_on_build"
  Revert "Merge pull request #12443 from arthurnn/add_inverse_of_add_target"
  bumping to rc2
  Merge pull request #12443 from arthurnn/add_inverse_of_add_target
  bumping version to 3.2.15.rc1
  Fix STI scopes using benolee's suggestion. Fixes #11939
eb8807e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.