-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
polymorphic association issue with presetted "source_type" #5434
Comments
The SELECT "bars".* FROM "bars" WHERE "bars"."source_id" = 1 AND "bars"."source_type" = 'Foo' AND "bars"."source_type" = 'CustomType' I can't see how that association would ever return anything other than an empty array. This is on Rails 3.2, if by chance you are using a older version that doesn't have the commit 9c023cc (i.e 3.0) then one of the conditions overwrites the other - I can't remember which. Perhaps the bug you're trying to demonstrate got lost in translation to abstract code? |
Yeap, one line is missed It is actually not a bug. It is looks like a hack. If conventionally polymorphic associations MUST always store class name in Or. If we want to give the ability of rewriting this field, so we shouldn't rewrite it while saving parent object. I mean if we have already set field with some default value, save process shouldn't rewrite with class name, and if the field is blank, so it must write class name there. What I mean. In first case when we protect our foo = Foo.new
bar = foo.bars.build
bar.source_type
#=> "Foo", not "CustomType"! And warning 'You are trying to set protected polymorphic field blah blah blah. Rejected' In second case foo = Foo.new
bar = foo.bars.build
foo.bars << bar
foo.save
foo.bars.first.source_type
#=> "CustomType" I think it should be protected from setting it in |
@fl00r is this closed then? Seems you've pretty much answered your own question. I think you may have just ran into an edge in polymorphic associations. |
When I preset in my has_many association it's type (as a condition option) It will be rewriten if I save it in scope with root object, while if I save it separetly it will save it right.
The text was updated successfully, but these errors were encountered: