This project is archived and is in readonly mode.
Backtracking association proxy chained methods
-
Diego Algorta
Maybe you'd like to test if the patch in #1960 Make named_scopes remember the current scope when defined fixes this problem? Haven't had the time to test your diff there.
-
Jonathan Garvin
Thanks for the suggestion Diego, but when I apply your patch, my test attached above still fails, and a new test fails ( test_named_scope(DefaultScopingTest) ).
I don't think the issue I'm dealing with has is specific to named scopes but instead to associations proxies in general.
-
Ryan Berdeen
In fact, the same problem occurs with named scopes and class methods, but it's the basically the inverse of #1960 Make named_scopes remember the current scope when defined (I thought they were dups at first, sorry for the confusion). Class methods called through association proxies and named scopes are both evaluated within the context of a with_scope. Adding
named_scope :having_author_id, lambda { |author_id| {:conditions => ['`categorizations`.author_id = ?', author_id]} }to Categorization and then doing
count_without_class_method = authors(:david).categorizations.find(:all).select {|cz| cz.category.categorizations.size > 1 }.size count_with_class_method = Categorization.having_author_id(authors(:david).id).that_have_siblings_with_common_category_parent.size assert_equal(count_without_class_method,count_with_class_method)gives the same result as accessing it through the association_collection.
Either way, to get the result you want, just put the
cz.category.categorizations.sizewithin an exclusive scope:def that_have_siblings_with_common_category_parent find(:all).select {|cz| with_exclusive_scope {cz.category.categorizations.size > 1} } end With this, the test passes. -
Jonathan Garvin
Thanks Ryan, that's good to know. But I wonder if that should be considered the "correct" way to do that, or is that a hack/workaround/band-aid? I'm inclined to think it's a hack and we'd be better off solving the underlying problem (though, I don't know what that would be, yet). However, I'd rather be proven wrong before I waste any time trying to "fix" something that nobody else wants fixed. ;-)
-
Santiago Pastorino
- State changed from new to open
- Importance changed from to
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino
- State changed from open to stale
