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

Projects
None yet
8 participants
@yui-knk
Contributor

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

This comment has been minimized.

rails-bot commented Feb 2, 2016

r? @rafaelfranca

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

@arthurnn

This comment has been minimized.

Member

arthurnn commented Oct 28, 2016

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

@mxie

This comment has been minimized.

Contributor

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

This comment has been minimized.

Member

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

This comment has been minimized.

Contributor

yui-knk commented Nov 4, 2016

@sgrif Rebased!

Automatically guess the inverse associations for STI
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.

@amatsuda amatsuda merged commit 02b5453 into rails:master Aug 22, 2017

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