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

Automatically guess the inverse associations for STI #23425

Merged
merged 1 commit into from Aug 22, 2017

Conversation

@yui-knk
Copy link
Contributor

@yui-knk yui-knk commented Feb 2, 2016

ActiveRecord associations automatically guess the inverse associations.
But this feature does not work correctly on assoctions for STI.
For example, before this commit

class Post < ActiveRecord::Base
  belongs_to :author
end

class SpecialPost < Post; end

class Author < ActiveRecord::Base
  has_many :posts
  has_many :special_posts
end

author.posts.first.author works correctly, but
author.special_posts.first.author does not work correctly.

@rails-bot
Copy link

@rails-bot rails-bot commented Feb 2, 2016

r? @rafaelfranca

(@rails-bot has picked a reviewer for you, use r? to override)

@yui-knk yui-knk force-pushed the yui-knk:support_sti_automatically_inverse branch Mar 25, 2016
@arthurnn
Copy link
Member

@arthurnn arthurnn commented Oct 28, 2016

at a first pass this looks correct.
@rafaelfranca @sgrif would you mind also taking a look?

@yui-knk yui-knk force-pushed the yui-knk:support_sti_automatically_inverse branch Oct 31, 2016
@mxie
Copy link
Contributor

@mxie mxie commented Nov 2, 2016

It'd be awesome to get this merged sometime soon. This is the exact behavior I would've expected when interacting with a child record. Instead, for a setup like this:

class Challenge < ApplicationRecord
  has_many :questions
end

class Question < ApplicationRecord
  belongs_to :challenge
end

class MultipleChoiceQuestion < Question
  # where `type` == "MultipleChoiceQuestion"
end

the following

challenge = Challenge.new
MultipleChoiceQuestion.create(challenge: challange)

would give me: ActiveRecord::AssociationNotFoundError: Association named 'challenge' was not found on Assessments::RankingQuestion; perhaps you misspelled it?

It seems to know that it's an association, but not really...

@sgrif sgrif self-assigned this Nov 2, 2016
@sgrif
Copy link
Contributor

@sgrif sgrif commented Nov 2, 2016

@yui-knk This looks fine to me. Can you rebase?

@maclover7 maclover7 added the needs work label Nov 3, 2016
@yui-knk yui-knk force-pushed the yui-knk:support_sti_automatically_inverse branch Nov 4, 2016
@yui-knk
Copy link
Contributor Author

@yui-knk yui-knk commented Nov 4, 2016

@sgrif Rebased!

@yui-knk yui-knk force-pushed the yui-knk:support_sti_automatically_inverse branch 2 times, most recently Nov 7, 2016
@yui-knk yui-knk force-pushed the yui-knk:support_sti_automatically_inverse branch Nov 17, 2016
@yui-knk yui-knk force-pushed the yui-knk:support_sti_automatically_inverse branch 2 times, most recently Jul 16, 2017
ActiveRecord associations automatically guess the inverse associations.
But this feature does not work correctly on assoctions for STI.
For example, before this commit

```
class Post < ActiveRecord::Base
  belongs_to :author
end

class SpecialPost < Post; end

class Author < ActiveRecord::Base
  has_many :posts
  has_many :special_posts
end
```

`author.posts.first.author` works correctly, but
`author.special_posts.first.author` does not work correctly.
@yui-knk yui-knk force-pushed the yui-knk:support_sti_automatically_inverse branch to 30ef715 Aug 22, 2017
@amatsuda amatsuda merged commit 02b5453 into rails:master Aug 22, 2017
1 of 2 checks passed
1 of 2 checks passed
continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
codeclimate All good!
Details
@yui-knk yui-knk deleted the yui-knk:support_sti_automatically_inverse branch Aug 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

8 participants