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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Rafael Mendonça França
2013-May-30 15:15 UTC
Re: [ActiveRecord] Bang version of named scopes
`merge!` and all the bang methods are private API and SHOULD NOT be used by the final users. This is why they are marked as `nodoc`. For the same reason we changed these method to be private API, I think we are not interested on this feature. See https://github.com/rails/rails/commit/c5bdf6c5aee61848bee67b307287e2f28ddca173 Rafael Mendonça França http://twitter.com/rafaelfranca https://github.com/rafaelfranca On Thu, May 30, 2013 at 12:12 PM, Радослав Станков <rstankov@gmail.com>wrote:> 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 rubyonrails-core+unsubscribe@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- 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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Zolzaya Erdenebaatar
2013-May-30 15:15 UTC
Re: [ActiveRecord] Bang version of named scopes
+1 on your idea ;) On May 30, 2013 11:12 PM, "Радослав Станков" <rstankov@gmail.com> wrote:> 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 rubyonrails-core+unsubscribe@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- 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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.