Gary Weaver
2013-Oct-30 15:51 UTC
bang query methods (where!, includes!, etc.) cause indeciperable errors when called on model class
I understand that bang methods in the classic Ruby sense are supposed to alter the current instance, but getting errors like: NoMethodError: undefined method `where!'' for #<Class:0x007ff1522d3110> and NoMethodError: undefined method `includes!'' for #<Class:0x007ff1522d3110> when calling the bang methods from QueryMethods on a model class are really not helpful. It''s understandable that you don''t want to pass a model class into a method that expects a relation that it can act upon and change with a bang query method and have it successfully create a new relation and call some method on it that is never again seen, hence the error. I also understand wanting to avoid any more checking/raising code than necessary in ActiveRecord both for maintainability, clarity, and maybe efficiency- but just look at this: 2.0.0p247 :001 > MyModel.to_s => "MyModel" 2.0.0p247 :002 > MyModel.new.to_s => "#< MyModel:0x007fc92aa017e8>" 2.0.0p247 :003 > MyModel.where({}) => ... 2.0.0p247 :003 > MyModel.where!({}) NoMethodError: undefined method `where!'' for #<Class:0x007fc92a95a240> Thanks! -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Rafael Mendonça França
2013-Oct-30 16:02 UTC
Re: bang query methods (where!, includes!, etc.) cause indeciperable errors when called on model class
The bang methods are private and should not be used in applications (see the :nodoc: in the method definition) Rafael Mendonça França http://twitter.com/rafaelfranca https://github.com/rafaelfranca On Wed, Oct 30, 2013 at 1:51 PM, Gary Weaver <garysweaver@gmail.com> wrote:> I understand that bang methods in the classic Ruby sense are supposed to > alter the current instance, but getting errors like: > > NoMethodError: undefined method `where!'' for #<Class:0x007ff1522d3110> > > and > > NoMethodError: undefined method `includes!'' for #<Class:0x007ff1522d3110> > > when calling the bang methods from QueryMethods on a model class are > really not helpful. > > It''s understandable that you don''t want to pass a model class into a > method that expects a relation that it can act upon and change with a bang > query method and have it successfully create a new relation and call some > method on it that is never again seen, hence the error. I also understand > wanting to avoid any more checking/raising code than necessary in > ActiveRecord both for maintainability, clarity, and maybe efficiency- but > just look at this: > > 2.0.0p247 :001 > MyModel.to_s > => "MyModel" > 2.0.0p247 :002 > MyModel.new.to_s > => "#< MyModel:0x007fc92aa017e8>" > 2.0.0p247 :003 > MyModel.where({}) > => ... > 2.0.0p247 :003 > MyModel.where!({}) > NoMethodError: undefined method `where!'' for #<Class:0x007fc92a95a240> > > Thanks! > > -- > 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. > 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. For more options, visit https://groups.google.com/groups/opt_out.
Gabriel Sobrinho
2013-Nov-04 13:53 UTC
Re: bang query methods (where!, includes!, etc.) cause indeciperable errors when called on model class
França, There is a reason for that? They sound useful in a lot of scenarios. On Wednesday, October 30, 2013 2:02:54 PM UTC-2, Rafael Mendonça França wrote:> > The bang methods are private and should not be used in applications (see > the :nodoc: in the method definition) > > Rafael Mendonça França > http://twitter.com/rafaelfranca > https://github.com/rafaelfranca > > > On Wed, Oct 30, 2013 at 1:51 PM, Gary Weaver <garys...@gmail.com<javascript:> > > wrote: > >> I understand that bang methods in the classic Ruby sense are supposed to >> alter the current instance, but getting errors like: >> >> NoMethodError: undefined method `where!'' for #<Class:0x007ff1522d3110> >> >> and >> >> NoMethodError: undefined method `includes!'' for #<Class:0x007ff1522d3110> >> >> when calling the bang methods from QueryMethods on a model class are >> really not helpful. >> >> It''s understandable that you don''t want to pass a model class into a >> method that expects a relation that it can act upon and change with a bang >> query method and have it successfully create a new relation and call some >> method on it that is never again seen, hence the error. I also understand >> wanting to avoid any more checking/raising code than necessary in >> ActiveRecord both for maintainability, clarity, and maybe efficiency- but >> just look at this: >> >> 2.0.0p247 :001 > MyModel.to_s >> => "MyModel" >> 2.0.0p247 :002 > MyModel.new.to_s >> => "#< MyModel:0x007fc92aa017e8>" >> 2.0.0p247 :003 > MyModel.where({}) >> => ... >> 2.0.0p247 :003 > MyModel.where!({}) >> NoMethodError: undefined method `where!'' for #<Class:0x007fc92a95a240> >> >> Thanks! >> >> -- >> 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-co...@googlegroups.com <javascript:>. >> To post to this group, send email to rubyonra...@googlegroups.com<javascript:> >> . >> Visit this group at http://groups.google.com/group/rubyonrails-core. >> 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. For more options, visit https://groups.google.com/groups/opt_out.
Rafael Mendonça França
2013-Nov-04 15:01 UTC
Re: bang query methods (where!, includes!, etc.) cause indeciperable errors when called on model class
Yes, there is https://github.com/rails/rails/commit/8c2c60511beaad05a218e73c4918ab89fb1804f0#commitcomment-2211685 Rafael Mendonça França http://twitter.com/rafaelfranca https://github.com/rafaelfranca On Mon, Nov 4, 2013 at 11:53 AM, Gabriel Sobrinho < gabriel.sobrinho@gmail.com> wrote:> França, > > There is a reason for that? They sound useful in a lot of scenarios. > > On Wednesday, October 30, 2013 2:02:54 PM UTC-2, Rafael Mendonça França > wrote: > >> The bang methods are private and should not be used in applications (see >> the :nodoc: in the method definition) >> >> Rafael Mendonça França >> http://twitter.com/rafaelfranca >> https://github.com/rafaelfranca >> >> >> On Wed, Oct 30, 2013 at 1:51 PM, Gary Weaver <garys...@gmail.com> wrote: >> >>> I understand that bang methods in the classic Ruby sense are supposed >>> to alter the current instance, but getting errors like: >>> >>> NoMethodError: undefined method `where!'' for #<Class:0x007ff1522d3110> >>> >>> and >>> >>> NoMethodError: undefined method `includes!'' for #<Class:0x007ff1522d3110> >>> >>> when calling the bang methods from QueryMethods on a model class are >>> really not helpful. >>> >>> It''s understandable that you don''t want to pass a model class into a >>> method that expects a relation that it can act upon and change with a bang >>> query method and have it successfully create a new relation and call some >>> method on it that is never again seen, hence the error. I also understand >>> wanting to avoid any more checking/raising code than necessary in >>> ActiveRecord both for maintainability, clarity, and maybe efficiency- but >>> just look at this: >>> >>> 2.0.0p247 :001 > MyModel.to_s >>> => "MyModel" >>> 2.0.0p247 :002 > MyModel.new.to_s >>> => "#< MyModel:0x007fc92aa017e8>" >>> 2.0.0p247 :003 > MyModel.where({}) >>> => ... >>> 2.0.0p247 :003 > MyModel.where!({}) >>> NoMethodError: undefined method `where!'' for #<Class:0x007fc92a95a240> >>> >>> Thanks! >>> >>> -- >>> 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-co...@googlegroups.com. >>> To post to this group, send email to rubyonra...@googlegroups.com. >>> >>> Visit this group at http://groups.google.com/group/rubyonrails-core. >>> 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. > 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. For more options, visit https://groups.google.com/groups/opt_out.
Gabriel Sobrinho
2013-Nov-05 12:55 UTC
Re: bang query methods (where!, includes!, etc.) cause indeciperable errors when called on model class
França, I''m too late to the party, I thought these bang methods was public on rails 4 :( We will have people doing wrong things with these methods but a good documentation will reduce that, ruby itself have a lot of these methods like params.merge!(..) or controller_name.gsub!(...) and I never saw people (that do not means there is no people reporting, I just don''t saw) reporting issues because things like that. The problem of scopes that must not be mutated can be solved using some freeze implementation on that scope, like this: relation = User.where(:dumb => false).immutable! relation.where!(:dumb => true) #=> raises ActiveRecord::ImmutableRelation I agree that new developers may do strange things but the rails itself is not so easy for beginners, but I have no expertise about how it may impact the community, so if you guys think that won''t be great these methods be public, :okaymeme: Gary, What I usually do when "mutable relations" makes sense are filters like that: https://gist.github.com/sobrinho/7318585 On Monday, November 4, 2013 1:01:59 PM UTC-2, Rafael Mendonça França wrote:> > Yes, there is > https://github.com/rails/rails/commit/8c2c60511beaad05a218e73c4918ab89fb1804f0#commitcomment-2211685 > > Rafael Mendonça França > http://twitter.com/rafaelfranca > https://github.com/rafaelfranca > > > On Mon, Nov 4, 2013 at 11:53 AM, Gabriel Sobrinho <gabriel....@gmail.com<javascript:> > > wrote: > >> França, >> >> There is a reason for that? They sound useful in a lot of scenarios. >> >> On Wednesday, October 30, 2013 2:02:54 PM UTC-2, Rafael Mendonça França >> wrote: >> >>> The bang methods are private and should not be used in applications (see >>> the :nodoc: in the method definition) >>> >>> Rafael Mendonça França >>> http://twitter.com/**rafaelfranca <http://twitter.com/rafaelfranca> >>> https://github.com/**rafaelfranca <https://github.com/rafaelfranca> >>> >>> >>> On Wed, Oct 30, 2013 at 1:51 PM, Gary Weaver <garys...@gmail.com> wrote: >>> >>>> I understand that bang methods in the classic Ruby sense are supposed >>>> to alter the current instance, but getting errors like: >>>> >>>> NoMethodError: undefined method `where!'' for #<Class:0x007ff1522d3110> >>>> >>>> and >>>> >>>> NoMethodError: undefined method `includes!'' for >>>> #<Class:0x007ff1522d3110> >>>> >>>> when calling the bang methods from QueryMethods on a model class are >>>> really not helpful. >>>> >>>> It''s understandable that you don''t want to pass a model class into a >>>> method that expects a relation that it can act upon and change with a bang >>>> query method and have it successfully create a new relation and call some >>>> method on it that is never again seen, hence the error. I also understand >>>> wanting to avoid any more checking/raising code than necessary in >>>> ActiveRecord both for maintainability, clarity, and maybe efficiency- but >>>> just look at this: >>>> >>>> 2.0.0p247 :001 > MyModel.to_s >>>> => "MyModel" >>>> 2.0.0p247 :002 > MyModel.new.to_s >>>> => "#< MyModel:0x007fc92aa017e8>"** >>>> 2.0.0p247 :003 > MyModel.where({}) >>>> => ... >>>> 2.0.0p247 :003 > MyModel.where!({}) >>>> NoMethodError: undefined method `where!'' for #<Class:0x007fc92a95a240> >>>> >>>> Thanks! >>>> >>>> -- >>>> 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-co...@**googlegroups.com. >>>> To post to this group, send email to rubyonra...@googlegroups.**com. >>>> >>>> Visit this group at http://groups.google.com/**group/rubyonrails-core<http://groups.google.com/group/rubyonrails-core> >>>> . >>>> For more options, visit https://groups.google.com/**groups/opt_out<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-co...@googlegroups.com <javascript:>. >> To post to this group, send email to rubyonra...@googlegroups.com<javascript:> >> . >> Visit this group at http://groups.google.com/group/rubyonrails-core. >> 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. For more options, visit https://groups.google.com/groups/opt_out.
Allen Madsen
2013-Nov-05 13:31 UTC
Re: bang query methods (where!, includes!, etc.) cause indeciperable errors when called on model class
The primary reason I would be against these being public is the following example: User.where!(email: ''bob@example.com'') Reading that, I would expect that I have changed the default scope for EVERY user query. I believe this is what José Valim was hinting at. Because the ActiveRecord class is a global object, scopes originate from there, and any piece of code could modify the behavior of that global, it would introduce side effects throughout an application. Ideally, when side effects are present, they''re localized to a small scope. Allen Madsen http://www.allenmadsen.com On Tue, Nov 5, 2013 at 7:55 AM, Gabriel Sobrinho <gabriel.sobrinho@gmail.com> wrote:> França, > > I''m too late to the party, I thought these bang methods was public on > rails 4 :( > > We will have people doing wrong things with these methods but a good > documentation will reduce that, ruby itself have a lot of these methods > like params.merge!(..) or controller_name.gsub!(...) and I never saw people > (that do not means there is no people reporting, I just don''t saw) > reporting issues because things like that. > > The problem of scopes that must not be mutated can be solved using some > freeze implementation on that scope, like this: > > relation = User.where(:dumb => false).immutable! > relation.where!(:dumb => true) > #=> raises ActiveRecord::ImmutableRelation > > I agree that new developers may do strange things but the rails itself is > not so easy for beginners, but I have no expertise about how it may impact > the community, so if you guys think that won''t be great these methods be > public, :okaymeme: > > > Gary, > > What I usually do when "mutable relations" makes sense are filters like > that: https://gist.github.com/sobrinho/7318585 > > > On Monday, November 4, 2013 1:01:59 PM UTC-2, Rafael Mendonça França wrote: > >> Yes, there is https://github.com/rails/rails/commit/ >> 8c2c60511beaad05a218e73c4918ab89fb1804f0#commitcomment-2211685 >> >> Rafael Mendonça França >> http://twitter.com/rafaelfranca >> https://github.com/rafaelfranca >> >> >> On Mon, Nov 4, 2013 at 11:53 AM, Gabriel Sobrinho <gabriel....@gmail.com>wrote: >> >>> França, >>> >>> There is a reason for that? They sound useful in a lot of scenarios. >>> >>> On Wednesday, October 30, 2013 2:02:54 PM UTC-2, Rafael Mendonça França >>> wrote: >>> >>>> The bang methods are private and should not be used in applications >>>> (see the :nodoc: in the method definition) >>>> >>>> Rafael Mendonça França >>>> http://twitter.com/rafaelfranca >>>> https://github.com/rafaelfranca >>>> >>>> >>>> On Wed, Oct 30, 2013 at 1:51 PM, Gary Weaver <garys...@gmail.com>wrote: >>>> >>>>> I understand that bang methods in the classic Ruby sense are >>>>> supposed to alter the current instance, but getting errors like: >>>>> >>>>> NoMethodError: undefined method `where!'' for #<Class:0x007ff1522d3110> >>>>> >>>>> and >>>>> >>>>> NoMethodError: undefined method `includes!'' for >>>>> #<Class:0x007ff1522d3110> >>>>> >>>>> when calling the bang methods from QueryMethods on a model class are >>>>> really not helpful. >>>>> >>>>> It''s understandable that you don''t want to pass a model class into a >>>>> method that expects a relation that it can act upon and change with a bang >>>>> query method and have it successfully create a new relation and call some >>>>> method on it that is never again seen, hence the error. I also understand >>>>> wanting to avoid any more checking/raising code than necessary in >>>>> ActiveRecord both for maintainability, clarity, and maybe efficiency- but >>>>> just look at this: >>>>> >>>>> 2.0.0p247 :001 > MyModel.to_s >>>>> => "MyModel" >>>>> 2.0.0p247 :002 > MyModel.new.to_s >>>>> => "#< MyModel:0x007fc92aa017e8>" >>>>> 2.0.0p247 :003 > MyModel.where({}) >>>>> => ... >>>>> 2.0.0p247 :003 > MyModel.where!({}) >>>>> NoMethodError: undefined method `where!'' for #<Class:0x007fc92a95a240> >>>>> >>>>> Thanks! >>>>> >>>>> -- >>>>> 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-co...@googlegroups.com. >>>>> To post to this group, send email to rubyonra...@googlegroups.com. >>>>> >>>>> Visit this group at http://groups.google.com/group/rubyonrails-core. >>>>> 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-co...@googlegroups.com. >>> To post to this group, send email to rubyonra...@googlegroups.com. >>> Visit this group at http://groups.google.com/group/rubyonrails-core. >>> 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. > 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. For more options, visit https://groups.google.com/groups/opt_out.
Gabriel Sobrinho
2013-Nov-05 13:40 UTC
Re: bang query methods (where!, includes!, etc.) cause indeciperable errors when called on model class
That’s why the proposal of immutable relations, User.where! would raise a ActiveRecord::ImmutableRelation but must be a way to create mutable relations, Model.all would be fine since it returns new relations on each call (not sure about internals but it seems not too difficulty to make it work): irb(main):012:0> Transaction.all.object_id => 70363740246020 irb(main):013:0> Transaction.all.object_id => 70363740260420 An “acceptable" API would be: User.where!(email: ‘bob@example.com’) #=> raise ActiveRecord::ImmutableRelation(“You can’t use bang relation methods directly on model, use User.all.where! instead”) User.all.where!(email: ‘bob@example.com’) #=> [] Cheers, Gabriel Sobrinho gabrielsobrinho.com On Nov 5, 2013, at 11:31 AM, Allen Madsen <allen.c.madsen@gmail.com> wrote:> The primary reason I would be against these being public is the following example: > > User.where!(email: ''bob@example.com'') > > Reading that, I would expect that I have changed the default scope for EVERY user query. I believe this is what José Valim was hinting at. Because the ActiveRecord class is a global object, scopes originate from there, and any piece of code could modify the behavior of that global, it would introduce side effects throughout an application. Ideally, when side effects are present, they''re localized to a small scope. > > Allen Madsen > http://www.allenmadsen.com > > > On Tue, Nov 5, 2013 at 7:55 AM, Gabriel Sobrinho <gabriel.sobrinho@gmail.com> wrote: > França, > > I''m too late to the party, I thought these bang methods was public on rails 4 :( > > We will have people doing wrong things with these methods but a good documentation will reduce that, ruby itself have a lot of these methods like params.merge!(..) or controller_name.gsub!(...) and I never saw people (that do not means there is no people reporting, I just don''t saw) reporting issues because things like that. > > The problem of scopes that must not be mutated can be solved using some freeze implementation on that scope, like this: > > relation = User.where(:dumb => false).immutable! > relation.where!(:dumb => true) > #=> raises ActiveRecord::ImmutableRelation > > I agree that new developers may do strange things but the rails itself is not so easy for beginners, but I have no expertise about how it may impact the community, so if you guys think that won''t be great these methods be public, :okaymeme: > > > Gary, > > What I usually do when "mutable relations" makes sense are filters like that: https://gist.github.com/sobrinho/7318585 > > > On Monday, November 4, 2013 1:01:59 PM UTC-2, Rafael Mendonça França wrote: > Yes, there is https://github.com/rails/rails/commit/8c2c60511beaad05a218e73c4918ab89fb1804f0#commitcomment-2211685 > > Rafael Mendonça França > http://twitter.com/rafaelfranca > https://github.com/rafaelfranca > > > On Mon, Nov 4, 2013 at 11:53 AM, Gabriel Sobrinho <gabriel....@gmail.com> wrote: > França, > > There is a reason for that? They sound useful in a lot of scenarios. > > On Wednesday, October 30, 2013 2:02:54 PM UTC-2, Rafael Mendonça França wrote: > The bang methods are private and should not be used in applications (see the :nodoc: in the method definition) > > Rafael Mendonça França > http://twitter.com/rafaelfranca > https://github.com/rafaelfranca > > > On Wed, Oct 30, 2013 at 1:51 PM, Gary Weaver <garys...@gmail.com> wrote: > I understand that bang methods in the classic Ruby sense are supposed to alter the current instance, but getting errors like: > > NoMethodError: undefined method `where!'' for #<Class:0x007ff1522d3110> > > and > > NoMethodError: undefined method `includes!'' for #<Class:0x007ff1522d3110> > > when calling the bang methods from QueryMethods on a model class are really not helpful. > > It''s understandable that you don''t want to pass a model class into a method that expects a relation that it can act upon and change with a bang query method and have it successfully create a new relation and call some method on it that is never again seen, hence the error. I also understand wanting to avoid any more checking/raising code than necessary in ActiveRecord both for maintainability, clarity, and maybe efficiency- but just look at this: > > 2.0.0p247 :001 > MyModel.to_s > => "MyModel" > 2.0.0p247 :002 > MyModel.new.to_s > => "#< MyModel:0x007fc92aa017e8>" > 2.0.0p247 :003 > MyModel.where({}) > => ... > 2.0.0p247 :003 > MyModel.where!({}) > NoMethodError: undefined method `where!'' for #<Class:0x007fc92a95a240> > > Thanks! > > > -- > 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-co...@googlegroups.com. > To post to this group, send email to rubyonra...@googlegroups.com. > > Visit this group at http://groups.google.com/group/rubyonrails-core. > 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-co...@googlegroups.com. > To post to this group, send email to rubyonra...@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core. > 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. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- > You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Core" group. > To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-core/4YusNsRI4Kk/unsubscribe. > To unsubscribe from this group and all its topics, 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. > 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. For more options, visit https://groups.google.com/groups/opt_out.
Rafael Mendonça França
2013-Nov-05 15:03 UTC
Re: bang query methods (where!, includes!, etc.) cause indeciperable errors when called on model class
I prefer to stick with that decision. It is easier to teach people when we say "Relations are immutables" than "Relations are immutables, unless you call these methods, or someone called they in some plugin". Documentation will help, but it will not solve. In the same way you didn''t read that these methods are private API (they are not even listed in the API site) people will not read the gotcha in the documentation. Also I don''t why we should complicate the relation API even more adding a new exception and new methods only to make possible to people able to call these bang methods. Rafael Mendonça França http://twitter.com/rafaelfranca https://github.com/rafaelfranca On Tue, Nov 5, 2013 at 10:55 AM, Gabriel Sobrinho < gabriel.sobrinho@gmail.com> wrote:> França, > > I''m too late to the party, I thought these bang methods was public on > rails 4 :( > > We will have people doing wrong things with these methods but a good > documentation will reduce that, ruby itself have a lot of these methods > like params.merge!(..) or controller_name.gsub!(...) and I never saw people > (that do not means there is no people reporting, I just don''t saw) > reporting issues because things like that. > > The problem of scopes that must not be mutated can be solved using some > freeze implementation on that scope, like this: > > relation = User.where(:dumb => false).immutable! > relation.where!(:dumb => true) > #=> raises ActiveRecord::ImmutableRelation > > I agree that new developers may do strange things but the rails itself is > not so easy for beginners, but I have no expertise about how it may impact > the community, so if you guys think that won''t be great these methods be > public, :okaymeme: > > > Gary, > > What I usually do when "mutable relations" makes sense are filters like > that: https://gist.github.com/sobrinho/7318585 > > > On Monday, November 4, 2013 1:01:59 PM UTC-2, Rafael Mendonça França wrote: > >> Yes, there is https://github.com/rails/rails/commit/ >> 8c2c60511beaad05a218e73c4918ab89fb1804f0#commitcomment-2211685 >> >> Rafael Mendonça França >> http://twitter.com/rafaelfranca >> https://github.com/rafaelfranca >> >> >> On Mon, Nov 4, 2013 at 11:53 AM, Gabriel Sobrinho <gabriel....@gmail.com>wrote: >> >>> França, >>> >>> There is a reason for that? They sound useful in a lot of scenarios. >>> >>> On Wednesday, October 30, 2013 2:02:54 PM UTC-2, Rafael Mendonça França >>> wrote: >>> >>>> The bang methods are private and should not be used in applications >>>> (see the :nodoc: in the method definition) >>>> >>>> Rafael Mendonça França >>>> http://twitter.com/rafaelfranca >>>> https://github.com/rafaelfranca >>>> >>>> >>>> On Wed, Oct 30, 2013 at 1:51 PM, Gary Weaver <garys...@gmail.com>wrote: >>>> >>>>> I understand that bang methods in the classic Ruby sense are >>>>> supposed to alter the current instance, but getting errors like: >>>>> >>>>> NoMethodError: undefined method `where!'' for #<Class:0x007ff1522d3110> >>>>> >>>>> and >>>>> >>>>> NoMethodError: undefined method `includes!'' for >>>>> #<Class:0x007ff1522d3110> >>>>> >>>>> when calling the bang methods from QueryMethods on a model class are >>>>> really not helpful. >>>>> >>>>> It''s understandable that you don''t want to pass a model class into a >>>>> method that expects a relation that it can act upon and change with a bang >>>>> query method and have it successfully create a new relation and call some >>>>> method on it that is never again seen, hence the error. I also understand >>>>> wanting to avoid any more checking/raising code than necessary in >>>>> ActiveRecord both for maintainability, clarity, and maybe efficiency- but >>>>> just look at this: >>>>> >>>>> 2.0.0p247 :001 > MyModel.to_s >>>>> => "MyModel" >>>>> 2.0.0p247 :002 > MyModel.new.to_s >>>>> => "#< MyModel:0x007fc92aa017e8>" >>>>> 2.0.0p247 :003 > MyModel.where({}) >>>>> => ... >>>>> 2.0.0p247 :003 > MyModel.where!({}) >>>>> NoMethodError: undefined method `where!'' for #<Class:0x007fc92a95a240> >>>>> >>>>> Thanks! >>>>> >>>>> -- >>>>> 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-co...@googlegroups.com. >>>>> To post to this group, send email to rubyonra...@googlegroups.com. >>>>> >>>>> Visit this group at http://groups.google.com/group/rubyonrails-core. >>>>> 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-co...@googlegroups.com. >>> To post to this group, send email to rubyonra...@googlegroups.com. >>> Visit this group at http://groups.google.com/group/rubyonrails-core. >>> 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. > 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. For more options, visit https://groups.google.com/groups/opt_out.
Gabriel Sobrinho
2013-Nov-05 15:08 UTC
Re: bang query methods (where!, includes!, etc.) cause indeciperable errors when called on model class
I see, makes sense, thanks! :) Cheers, Gabriel Sobrinho gabrielsobrinho.com On Nov 5, 2013, at 1:03 PM, Rafael Mendonça França <rafaelmfranca@gmail.com> wrote:> I prefer to stick with that decision. > > It is easier to teach people when we say "Relations are immutables" than "Relations are immutables, unless you call these methods, or someone called they in some plugin". > > Documentation will help, but it will not solve. In the same way you didn''t read that these methods are private API (they are not even listed in the API site) people will not read the gotcha in the documentation. > > Also I don''t why we should complicate the relation API even more adding a new exception and new methods only to make possible to people able to call these bang methods. > > Rafael Mendonça França > http://twitter.com/rafaelfranca > https://github.com/rafaelfranca > > > On Tue, Nov 5, 2013 at 10:55 AM, Gabriel Sobrinho <gabriel.sobrinho@gmail.com> wrote: > França, > > I''m too late to the party, I thought these bang methods was public on rails 4 :( > > We will have people doing wrong things with these methods but a good documentation will reduce that, ruby itself have a lot of these methods like params.merge!(..) or controller_name.gsub!(...) and I never saw people (that do not means there is no people reporting, I just don''t saw) reporting issues because things like that. > > The problem of scopes that must not be mutated can be solved using some freeze implementation on that scope, like this: > > relation = User.where(:dumb => false).immutable! > relation.where!(:dumb => true) > #=> raises ActiveRecord::ImmutableRelation > > I agree that new developers may do strange things but the rails itself is not so easy for beginners, but I have no expertise about how it may impact the community, so if you guys think that won''t be great these methods be public, :okaymeme: > > > Gary, > > What I usually do when "mutable relations" makes sense are filters like that: https://gist.github.com/sobrinho/7318585 > > > On Monday, November 4, 2013 1:01:59 PM UTC-2, Rafael Mendonça França wrote: > Yes, there is https://github.com/rails/rails/commit/8c2c60511beaad05a218e73c4918ab89fb1804f0#commitcomment-2211685 > > Rafael Mendonça França > http://twitter.com/rafaelfranca > https://github.com/rafaelfranca > > > On Mon, Nov 4, 2013 at 11:53 AM, Gabriel Sobrinho <gabriel....@gmail.com> wrote: > França, > > There is a reason for that? They sound useful in a lot of scenarios. > > On Wednesday, October 30, 2013 2:02:54 PM UTC-2, Rafael Mendonça França wrote: > The bang methods are private and should not be used in applications (see the :nodoc: in the method definition) > > Rafael Mendonça França > http://twitter.com/rafaelfranca > https://github.com/rafaelfranca > > > On Wed, Oct 30, 2013 at 1:51 PM, Gary Weaver <garys...@gmail.com> wrote: > I understand that bang methods in the classic Ruby sense are supposed to alter the current instance, but getting errors like: > > NoMethodError: undefined method `where!'' for #<Class:0x007ff1522d3110> > > and > > NoMethodError: undefined method `includes!'' for #<Class:0x007ff1522d3110> > > when calling the bang methods from QueryMethods on a model class are really not helpful. > > It''s understandable that you don''t want to pass a model class into a method that expects a relation that it can act upon and change with a bang query method and have it successfully create a new relation and call some method on it that is never again seen, hence the error. I also understand wanting to avoid any more checking/raising code than necessary in ActiveRecord both for maintainability, clarity, and maybe efficiency- but just look at this: > > 2.0.0p247 :001 > MyModel.to_s > => "MyModel" > 2.0.0p247 :002 > MyModel.new.to_s > => "#< MyModel:0x007fc92aa017e8>" > 2.0.0p247 :003 > MyModel.where({}) > => ... > 2.0.0p247 :003 > MyModel.where!({}) > NoMethodError: undefined method `where!'' for #<Class:0x007fc92a95a240> > > Thanks! > > > -- > 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-co...@googlegroups.com. > To post to this group, send email to rubyonra...@googlegroups.com. > > Visit this group at http://groups.google.com/group/rubyonrails-core. > 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-co...@googlegroups.com. > To post to this group, send email to rubyonra...@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core. > 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. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- > You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Core" group. > To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-core/4YusNsRI4Kk/unsubscribe. > To unsubscribe from this group and all its topics, 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. > 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. For more options, visit https://groups.google.com/groups/opt_out.
Gary Weaver
2013-Nov-05 16:46 UTC
Re: bang query methods (where!, includes!, etc.) cause indeciperable errors when called on model class
Thanks, Gabriel. The one place it was a pain, I just did array wrapping and unsplat to remedy, e.g. before I had: def some_method(relation, param_name) # ... other code relation.joins!(opts[:joins]) # ... opts.reverse_merge(attr_name: param_name.to_sym) end # in code that calls it... opts = some_method(relation, param_name) now I have: def some_method(relation, param_name) # ... other code relation = relation.joins(opts[:joins]) # ... [relation, opts.reverse_merge(attr_name: param_name.to_sym)] end # in code that calls it... relation, opts = *some_method(relation, param_name) It''s doing more than I''d like (having to create another array to wrap the result, and all of the additional operations in joins(...) vs. joins!(...)), but that''s ok. Gary On Tue, Nov 5, 2013 at 7:55 AM, Gabriel Sobrinho <gabriel.sobrinho@gmail.com> wrote:> França, > > I''m too late to the party, I thought these bang methods was public on > rails 4 :( > > We will have people doing wrong things with these methods but a good > documentation will reduce that, ruby itself have a lot of these methods > like params.merge!(..) or controller_name.gsub!(...) and I never saw people > (that do not means there is no people reporting, I just don''t saw) > reporting issues because things like that. > > The problem of scopes that must not be mutated can be solved using some > freeze implementation on that scope, like this: > > relation = User.where(:dumb => false).immutable! > relation.where!(:dumb => true) > #=> raises ActiveRecord::ImmutableRelation > > I agree that new developers may do strange things but the rails itself is > not so easy for beginners, but I have no expertise about how it may impact > the community, so if you guys think that won''t be great these methods be > public, :okaymeme: > > > Gary, > > What I usually do when "mutable relations" makes sense are filters like > that: https://gist.github.com/sobrinho/7318585 > > > On Monday, November 4, 2013 1:01:59 PM UTC-2, Rafael Mendonça França wrote: > >> Yes, there is https://github.com/rails/rails/commit/ >> 8c2c60511beaad05a218e73c4918ab89fb1804f0#commitcomment-2211685 >> >> Rafael Mendonça França >> http://twitter.com/rafaelfranca >> https://github.com/rafaelfranca >> >> >> On Mon, Nov 4, 2013 at 11:53 AM, Gabriel Sobrinho <gabriel....@gmail.com>wrote: >> >>> França, >>> >>> There is a reason for that? They sound useful in a lot of scenarios. >>> >>> On Wednesday, October 30, 2013 2:02:54 PM UTC-2, Rafael Mendonça França >>> wrote: >>> >>>> The bang methods are private and should not be used in applications >>>> (see the :nodoc: in the method definition) >>>> >>>> Rafael Mendonça França >>>> http://twitter.com/rafaelfranca >>>> https://github.com/rafaelfranca >>>> >>>> >>>> On Wed, Oct 30, 2013 at 1:51 PM, Gary Weaver <garys...@gmail.com>wrote: >>>> >>>>> I understand that bang methods in the classic Ruby sense are >>>>> supposed to alter the current instance, but getting errors like: >>>>> >>>>> NoMethodError: undefined method `where!'' for #<Class:0x007ff1522d3110> >>>>> >>>>> and >>>>> >>>>> NoMethodError: undefined method `includes!'' for >>>>> #<Class:0x007ff1522d3110> >>>>> >>>>> when calling the bang methods from QueryMethods on a model class are >>>>> really not helpful. >>>>> >>>>> It''s understandable that you don''t want to pass a model class into a >>>>> method that expects a relation that it can act upon and change with a bang >>>>> query method and have it successfully create a new relation and call some >>>>> method on it that is never again seen, hence the error. I also understand >>>>> wanting to avoid any more checking/raising code than necessary in >>>>> ActiveRecord both for maintainability, clarity, and maybe efficiency- but >>>>> just look at this: >>>>> >>>>> 2.0.0p247 :001 > MyModel.to_s >>>>> => "MyModel" >>>>> 2.0.0p247 :002 > MyModel.new.to_s >>>>> => "#< MyModel:0x007fc92aa017e8>" >>>>> 2.0.0p247 :003 > MyModel.where({}) >>>>> => ... >>>>> 2.0.0p247 :003 > MyModel.where!({}) >>>>> NoMethodError: undefined method `where!'' for #<Class:0x007fc92a95a240> >>>>> >>>>> Thanks! >>>>> >>>>> -- >>>>> 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-co...@googlegroups.com. >>>>> To post to this group, send email to rubyonra...@googlegroups.com. >>>>> >>>>> Visit this group at http://groups.google.com/group/rubyonrails-core. >>>>> 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-co...@googlegroups.com. >>> To post to this group, send email to rubyonra...@googlegroups.com. >>> Visit this group at http://groups.google.com/group/rubyonrails-core. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >> >> -- > You received this message because you are subscribed to a topic in the > Google Groups "Ruby on Rails: Core" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/rubyonrails-core/4YusNsRI4Kk/unsubscribe > . > To unsubscribe from this group and all its topics, 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. > 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. For more options, visit https://groups.google.com/groups/opt_out.
Xavier Noria
2013-Nov-06 19:01 UTC
Re: bang query methods (where!, includes!, etc.) cause indeciperable errors when called on model class
We plan to move the API to YARD, hopefully for 4.1 unless there is something unexpected in the transition. These would be use-cases for the @private tag I believe. The tag will convey user-level private visibility better than nodoc for those reading the source code. -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Gabriel Sobrinho
2013-Nov-06 22:58 UTC
Re: bang query methods (where!, includes!, etc.) cause indeciperable errors when called on model class
That’s great, thanks Xavier! Cheers, Gabriel Sobrinho gabrielsobrinho.com On Nov 6, 2013, at 5:01 PM, Xavier Noria <fxn@hashref.com> wrote:> We plan to move the API to YARD, hopefully for 4.1 unless there is something unexpected in the transition. > > These would be use-cases for the @private tag I believe. The tag will convey user-level private visibility better than nodoc for those reading the source code. > > > -- > You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Core" group. > To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-core/4YusNsRI4Kk/unsubscribe. > To unsubscribe from this group and all its topics, 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. > 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. For more options, visit https://groups.google.com/groups/opt_out.