-1 on inheritance solution. It is creative, but I imagine that can get messy very quickly if you have multiple attributes you're trying to do this with.
As for Luke's solution, I think it is good. But I do wonder if the interface should be different. Before named scopes it was often necessary to make custom associations with a :conditions hash. Now named scopes remove this need and offer a much more flexible solution. Going back to a custom association here with conditions (important_tags) seems to go against the grain of scopes to me. What if it were possible to use the :include option to include named scopes? It might look like this: class Product < ActiveRecord::Base named_scope :visible, :conditions => { :visible => true } named_scope :available, :conditions => { :available => true }, :include => :visible end This solves the problem of duplication across named scopes in one model, but how does this address your problem? Here's where it gets kind of cool. As you know, the :include option is also used to include associations. So what if you could nest named scope includes through associations? http://gist.github.com/71618 I'm not entirely sure how complex the implementation of this would be, but I would personally love to see this functionality. I know there was some discussion of this on Lighthouse some time ago, but I don't know what became of it and I cannot find it at the moment. What do you think? Ryan On Feb 27, 10:03 am, Duncan Beevers <duncanbeev...@gmail.com> wrote: > Instead of extracting a specific piece of a named_scope's proxy > options, I would probably elect to use a more flexible, > object-oriented solution using inheritance. > > http://gist.github.com/71604 > > > > On Fri, Feb 27, 2009 at 9:37 AM, Luke Redpath <ljredp...@gmail.com> wrote: > > > In my current project, I have an association model that has various > > flags available. To keep the implementation of these flags > > encapsulated, I created named_scopes for each flag and use these > > scopes for finding and creating. > > > This model sits between two others as the join model in a > > has_many :through association. I actually wanted various > > has_many :through associations that use the same join model but > > different scopes. Unfortunately has_many :through doesn't have > > a :scope option; I could use :conditions to get it working but I've > > now broken the encapsulation I aimed for when I introduced the named > > scopes and created unnecessary duplication. > > > Hopefully this rather contrived example should make it clear what I'm > > aiming for. > > >http://gist.github.com/71585 > > > And my initial implementation, currently in use in our app (tries to > > extend ActiveRecord in the least-intrusive way): > > >https://gist.github.com/9d7f86e27014ef5df280 > > > And now, my attempt to do it properly as a patch to ActiveRecord, with > > a test. > > >http://gist.github.com/71587 > > > I don't expect this patch to be completely ready for inclusion; there > > are probably other things to consider such as, do you just want to > > pull the :conditions from the proxy? Is there anything else to pull > > in? Could this be written in a better way (probably, my knowledge if > > the AR internals is slim). > > > Any thoughts? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---