Automatically guess the inverse associations for STI #23425
Merged
Conversation
(@rails-bot has picked a reviewer for you, use r? to override) |
at a first pass this looks correct. |
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: It seems to know that it's an association, but not really... |
@yui-knk This looks fine to me. Can you rebase? |
@sgrif Rebased! |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
ActiveRecord associations automatically guess the inverse associations.
But this feature does not work correctly on assoctions for STI.
For example, before this commit
author.posts.first.author
works correctly, butauthor.special_posts.first.author
does not work correctly.