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

STI cast new instances to default type on initialize. #17169

Merged
merged 1 commit into from Dec 2, 2015

Conversation

kuldeepaggarwal
Copy link
Contributor

fixes #17121

@kuldeepaggarwal
Copy link
Contributor Author

any update?

@rafaelfranca
Copy link
Member

Please don't ping in the issue tracker. It will be reviewed when someone have time and want to review.

@kuldeepaggarwal
Copy link
Contributor Author

Apology for creating noise in the issue tracker but its been 3 months. So, it was just a gentle reminder. I will take care of it.

@rafaelfranca
Copy link
Member

No problem.

Yeah, we are a lot behind the PRs schedule 😢. I'm trying to catch up, so expect this PR to being reviewed 🔜 😄

@kuldeepaggarwal
Copy link
Contributor Author

Thanks. 👍

@ryanrperez
Copy link
Contributor

I can verify that the added tests are correct and this is a fix for the issue.

@jormon
Copy link

jormon commented May 14, 2015

Workaround: in the class at the top of the inheritance hierarchy:

after_initialize do
    self.type = "Company" if self.class == Company
end

(for those too lazy to run off a patch) :/

@senny
Copy link
Member

senny commented Sep 10, 2015

@kuldeepaggarwal can we try to make a copy of attrs with inheritance_column => inheritance_column_default if inheritance_column is not already in attrs. We could possible do that here: https://github.com/kuldeepaggarwal/rails/blob/fix-STI-default-type/activerecord/lib/active_record/inheritance.rb#L53 and then subclass_from_attributes? and subclass_from_attributes can be left as is.

@kuldeepaggarwal
Copy link
Contributor Author

@senny Sure. will do it asap.

Thanks for the hint.

@kuldeepaggarwal
Copy link
Contributor Author

Updated the PR.

if subclass_from_attributes?(attrs)
attrs = attrs.with_indifferent_access
attrs[inheritance_column] ||= columns_hash[inheritance_column].default
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kuldeepaggarwal shouldn't this happen before if subclass_from_attributes?(attrs)? Otherwise subclass_from_attributes?(attrs) will not see the default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not actually seeing the defaults in subclass_from_attributes?(attrs),

def subclass_from_attributes?(attrs)
  attribute_names.include?(inheritance_column) && attrs.is_a?(Hash)
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 sorry, my bad. Misread attribute_names as attrs

@senny senny self-assigned this Sep 30, 2015
@senny
Copy link
Member

senny commented Nov 30, 2015

@kuldeepaggarwal sorry for the long delay 😓. Finally got around to cleaning up my inbox. This doesn't apply cleanly on master. Can you submit a rebased version?

@kuldeepaggarwal
Copy link
Contributor Author

@senny: Sure, give me sometime.

@kuldeepaggarwal
Copy link
Contributor Author

@senny Updated the PR.. 😄

end

def subclass_from_attributes(attrs)
attrs = attrs.to_h if attrs.respond_to?(:permitted?)
subclass_name = attrs.with_indifferent_access[inheritance_column]
subclass_name = (attrs || {}).with_indifferent_access[inheritance_column] || columns_hash[inheritance_column].default
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kuldeepaggarwal do we still need that ||. Previous code was calling with_indifferent_access directly, so that should never be nil, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, actually in my previous commit I was initializing attrs ||= {} so that's why I called with_indifferent_access directly, which is equivalent to current implementation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see. It's because we modify subclass_from_attributes? in a weird way. Why not keep subclass_from_attributes? as is but modify this part:

def new

        if subclass_from_attributes?(attrs)
          subclass = subclass_from_attributes(attrs)
        else
          subclass = columns_hash[inheritance_column].default
        end

This way the methods still do what their name suggests and it should be obvious at a glance how the subclass is determined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd still have to do the find_sti_class(subclass_name) though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified the implementation.

@kuldeepaggarwal kuldeepaggarwal force-pushed the fix-STI-default-type branch 2 times, most recently from 887e17d to 835c30d Compare December 2, 2015 10:38
@senny senny merged commit 6b18bdd into rails:master Dec 2, 2015
senny added a commit that referenced this pull request Dec 2, 2015
STI cast new instances to `default type` on initialize.
@senny
Copy link
Member

senny commented Dec 2, 2015

@kuldeepaggarwal thank you 💛

I made some modifications in the merge commit so that we can get rid of some duplicate conditionals.

@kuldeepaggarwal
Copy link
Contributor Author

Awesome..

@kuldeepaggarwal kuldeepaggarwal deleted the fix-STI-default-type branch December 2, 2015 12:56
senny added a commit that referenced this pull request Dec 2, 2015
This will also get the defaults from attribute definitions like:

     attribute :type, :string, default: "SomethingElse"
leonelgalan added a commit to leonelgalan/rails that referenced this pull request Jan 22, 2018
Fixes issue described in rails#30399: A default value on the
inheritance column prevented `child.becomes(Parent)` to return
an instance of `Parent` as expected, instead it returns an instance
of the default subclass.

The change was introduced by rails#17169 and it was meant to affect
initialization, alone. Where `Parent.new` is expected to return
an instance of the default subclass.
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.

Single table inheritance does not cast new instances to default type on initialize
6 participants