I really like that Rails 4 have ```where!``` methods. And I was expecting
that when I define a named scope I also get a bang version of the scope.
class Subscriber
scope :active, -> { where :active }
end
subscribers = Subscriber.all
subscribers.active!
Most of my ActiveRecord calls are through named scopes and I have to
redefine my scopes twice.
The quick and dirty way to add this to way todo this is to add this code
after
active_record/scoping/named.rb:170<https://github.com/rails/rails/blob/master/activerecord/lib/active_record/scoping/named.rb#L161-L170>
:
singleton_class.send(:define_method, "#{name}!") do |*args|
raise ImmutableRelation unless current_scope
current_scope.merge! send(name, *args)
end
Here a a gist with the code samples
- https://gist.github.com/RStankov/5678617
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.