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

r? @rafaelfranca

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

@arthurnn
Copy link
Member

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

@yui-knk yui-knk force-pushed the support_sti_automatically_inverse branch from d3f77f0 to ee404fd Compare October 31, 2016 01:00
@mxie
Copy link
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
Copy link
Contributor

sgrif commented Nov 2, 2016

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

@yui-knk yui-knk force-pushed the support_sti_automatically_inverse branch from ee404fd to 67ffba0 Compare November 4, 2016 00:46
@yui-knk
Copy link
Contributor Author

yui-knk commented Nov 4, 2016

@sgrif Rebased!

@yui-knk yui-knk force-pushed the support_sti_automatically_inverse branch 2 times, most recently from a86bd1f to d99ba10 Compare November 7, 2016 09:26
@yui-knk yui-knk force-pushed the support_sti_automatically_inverse branch from d99ba10 to 11c9be2 Compare November 17, 2016 08:43
@yui-knk yui-knk force-pushed the support_sti_automatically_inverse branch 2 times, most recently from 887c51d to 2730095 Compare July 16, 2017 01:32
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 support_sti_automatically_inverse branch from 2730095 to 30ef715 Compare August 22, 2017 09:57
@amatsuda amatsuda merged commit 02b5453 into rails:master Aug 22, 2017
@yui-knk yui-knk deleted the support_sti_automatically_inverse branch August 22, 2017 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants