I would really like to see default_scope go a step further by adding
optional 'names' to them. Since multiple default_scopes can be added
to model, it would be nice to selectively disable them by name.
Imagine the case for implementing a soft-delete gem:
default_scope :non_deleted, { where('deleted_at is not null') }
scope :with_deleted, { without_default(:non_deleted) }
message.all # returns all non_deleted records
message.with_deleted.all # returns all records, including those marked
as deleted
On Mar 6, 11:35 am, Adam Wróbel <[email protected]> wrote:
> I haven't thought about a possible transition plan yet. I'd like to hear more
> opinions about this idea. Syntax change is not a minor thing after all.
>
> You're also right, you need parentheses if you want to pass "{" block along
> with some arguments. Which is too bad, because it looks even worse with them.
>
> As for your question about returning lambda from within a block. I've
> corrected myself in the first reply to this thread:
>
>
>
>
>
>
>
> >>> On Fri, 2011-03-04 at 11:37 -0800, Adam Wróbel wrote:
> >>>> To solve the default scope problem it's enough to execute the blocks
> >>>> at the `unscoped` level, but if we also want to solve the issue shown
> >>>> in example 4 we need to delay block execution until default_scope or
> >>>> named scopes are used. Then of course returning lambdas from the block
> >>>> is unnecessary and a proper code would look a bit cleaner than what I
> >>>> have shown above:
>
> >>>> # example 3
> >>>> class Post < ActiveRecord::Base
> >>>> default_scope { where( :locale => Locales.current ) }
> >>>> scope :valid { where( :valid => true ) }
> >>>> end
>
> >>>> And:
>
> >>>> # example 5
> >>>> class Product < ActiveRecord::Base
> >>>> scope :not_deleted { where("products.deleted_at is NULL") }
> >>>> scope :available do |*on|
> >>>> where("products.available_on <= ?", on.first ||
> >>>> Time.zone.now )
> >>>> end
> >>>> scope :active { not_deleted.available }
> >>>> end
>
> >>>> Adam
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.