-
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
has_many with condition is not scoped on create, only for update #12464
Comments
May you provide test case for this issue by using https://github.com/rails/rails/blob/master/guides/bug_report_templates/action_controller_gem.rb or https://github.com/rails/rails/blob/master/guides/bug_report_templates/action_controller_master.rb I think there are some duplicated issues, one of them is #11535, but will be great to see your test case to confirm that. |
I was not able to replicate this issue (i.e. the scope from the has-many seems to work fine under both create and update). However, I noticed that as mentioned in #11535, the default scope of an object will overwrite the has-many scope, which may or may not be desirable... i.e. where class Customer < ActiveRecord::Base
has_many :confirmed_orders, -> { where confirmed: true },
class_name: "Order"
end if you have a Customer object "buyer", buyer.confirmed_orders.create will generate an Order object where "confirmed" is true. However, if within the Order class we have class Order < ActiveRecord::Base
default_scope { where confirmed: false }
end creating an order through the buyer will have "confirmed" be false rather than true. |
@jyao6 can you check the behavior of 3.2.x about the default scope? |
@rafaelfranca so I checked with 3.2.x, and it seems that the default scope still overrides the has-many scope So the behavior seems to be consistent, and it's just a matter of deciding what the correct behavior should be. Another mentor raised the issue of such a change affecting the merge operation. What do you think? |
@jyao6 I think the correct behavior would be doing a merge. So for example if we have: class Customer < ActiveRecord::Base
has_many :confirmed_orders, -> { where confirmed: true },
class_name: "Order"
end
class Order < ActiveRecord::Base
default_scope { where active: true }
end So But, in this case: class Customer < ActiveRecord::Base
has_many :confirmed_orders, -> { where confirmed: true },
class_name: "Order"
end
class Order < ActiveRecord::Base
default_scope { where confirmed: false }
end
But this goes against the ideas discussed at #13875. Specifically at #13875 (comment). That discussion is about what is the intended behavior of class Developer < ActiveRecord::Base
default_scope { where(name: 'David') }
scope :jamis, -> { where(name: 'Jamis') }
end Seems to have a consensus that this call should generate the following query: SELECT * FROM developers WHERE name = 'David' AND name = 'Jamis' And it is the same as In our case, if we think that @jonleighton thoughts? |
@rafaelfranca okay, let me know if there is anything I should look into changing (i.e. do you want me to try to figure out how to create the behavior you described above?) |
@jyao6 this is a good start. We could use the same behavior of |
@rafaelfranca #11535 resolves this issue, with the same idea, may you recheck it again? |
@rafaelfranca I don't have anything particular to add, except that yes I think we should AND both conditions for consistency. the proper way to write this would then be: class Customer < ActiveRecord::Base
has_many :confirmed_orders, -> { unscope(where: :confirmed).where confirmed: true },
class_name: "Order"
end (I don't think that will work til 4.1 though.) |
This issue has been automatically marked as stale because it has not been commented on for at least The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the Thank you for all your contributions. |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
Hi,
using Rails 4.0.0
the guide say this:
for code like this
but in my app this is only when updating the model, if i create it, it is not scoped so the confirmed attribute is nil.
My code looks like this:
When i add a User to users or managers via update, the manager attribute is filled correctly. On create, it is not.
Maybe there is bug or I'm doing it wrong.
The text was updated successfully, but these errors were encountered: