This project is archived and is in readonly mode.
Allow arbitrary named_scope instances to be merged
-
James Le Cuirot
Here's the same patch for 2.1 if anyone wants it.
-
James Le Cuirot
And again for 2.2.
-
CancelProfileIsBroken
- Tag changed from activerecord, edge, enhancement, named_scope, patch to activerecord, bugmash, edge, enhancement, named_scope, patch
-
Dan Pickett
+1 for the feature - I've definitely run into use cases where I've wanted this, but I'm not sure I agree with the implementation
Also, I could not merge this into 2-3-stable cleanly
I'm not wowed by the implementation - maybe we need a way to chain them ie. Shirt.red.and.dry_clean_only or Shirt.red_and_dry_clean_only
-
James Le Cuirot
I'll try and post a new patch today. I know that this works with 2.3. I'll have a think about your suggestion.
-
James Le Cuirot
Here's the patch against 2-3-stable.
-
James Le Cuirot
Dan, had a think about your suggestion but it doesn't really make sense. It's already possible to do stuff like Shirt.red.dry_clean_only. What I'm asking for is the ability to merge two already-existing scope instances. In my example, you can't just do "z = x.y" because that would be treating y as a method name, not a local variable name.
-
Elad Meidar
@james, Not clear what you are after.
You can chain named_scopes for a while now, but you want AND on the return record set ?
-
John Trupiano
@Elad, I think he's looking for the union of two named scopes. So if we had a named scope to return all green M&M's and a named scope to return all red M&M's, he'd want to be able to quickly get the collection of M&M's that are either red or green (the union of the two sets).
-
James Le Cuirot
I'm not sure I'd use the term "union" because conditions in one scope might remove records included by the other scope. I just want to merge two scopes together, like you do with chaining (it would give the same result), but for scopes that have already been assigned to variables. It's difficult to give a useful example.
-
Matt Jones
The name doesn't make a whole lot of sense - I was really hoping you'd written an implementation of scope union (joining with OR) rather than this.
This is also going (I think) to fail oddly if you try to merge in a previously chained scope:
x = Shirt.red.cotton y = Shirt.dry_clean_only z = y.merge(x)Because of that, I'm not sure what the advantage of merge over just chaining the scopes properly in the first place is.
Note that the most common case for this (search scopes and such) can work like this:
proxy = Shirt [:red, :cotton, :dry_clean_only].each do |s| proxy = proxy.send(s) unless params[s].blank? end @shirts = proxy.all # or paginate - whateverMaybe you could give a more convincing use case where chaining doesn't work?
-
David Trasbo
verified
Patch applies cleanly to 2-3-stable. I assume the tests are passing, I'm having some problems getting Ruby 1.9 and 1.8 play nicely. +1 on the idea, it may need more tests, though.
-
Elad Meidar
I agree with Matt, i don't see where this #merge is going to be any different from chaining scopes.
-
José Valim
- Tag changed from activerecord, bugmash, edge, enhancement, named_scope, patch to activerecord, edge, enhancement, named_scope, patch
- State changed from new to wontfix
This is a new feature that won't be added to Rails 2.3. For Rails 3.0 (master), Emilio (miloops) is already working on something similar in his GSoC project, since he's adding relations to ActiveRecord.
-
James Le Cuirot
That's fair enough. Thanks for the info.
