Hello, Sorry if this has been still answered, I haven''t found nothing on it. I would love to know why ActiveRecord::Base#last doesn''t return an ActiveRecord::Relation just like all or where since an ActiveRecord::Relation can act more or less like an array (as specified here<https://github.com/rails/rails/commit/0a6833b6f701c8c8febadfe2f45e25df29493602> )? Thanks, have a nice day. -- 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.
Mohamed Wael Khobalatte
2013-Jul-19 14:38 UTC
Re: Why last doesn''t return an ActiveRecord::Relation
Because you won''t need to run a query on one element? On Wed, Jul 17, 2013 at 11:33 AM, Robin Dupret <robin.dupret@gmail.com>wrote:> Hello, > > Sorry if this has been still answered, I haven''t found nothing on it. I > would love to know why ActiveRecord::Base#last doesn''t return an > ActiveRecord::Relation just like all or where since an > ActiveRecord::Relation can act more or less like an array (as specified > here<https://github.com/rails/rails/commit/0a6833b6f701c8c8febadfe2f45e25df29493602> > )? > > Thanks, have a nice day. > > -- > 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. > > >-- Mohamed Wael Khobalatte -- 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.
On Jul-19-2013, at 8:08 PM, Mohamed Wael Khobalatte <wael.khobalatte@gmail.com> wrote:> Because you won''t need to run a query on one element?I think the question is more about Model.last(5) or Model.first(5) These return arrays when they could return a relation. -- Tejas Dinkar http://www.nilenso.com Nilenso Software (formerly C42 Engineering) -- 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.
"first" and "last" have always returned an instance of the model. These are meant to be the final method that you call on a relation, to get the result. If you call "last(5)" it will return an array, and that''s the correct behaviour since that''s the result of the query. If you need the relation so you can add other criteria on top of it, you can still use "limit(5).order(arel_table[:id].desc)" Andrew Vit On Wednesday, July 17, 2013 8:33:52 AM UTC-7, Robin Dupret wrote:> > Hello, > > Sorry if this has been still answered, I haven''t found nothing on it. I > would love to know why ActiveRecord::Base#last doesn''t return an > ActiveRecord::Relation just like all or where since an > ActiveRecord::Relation can act more or less like an array (as specified > here<https://github.com/rails/rails/commit/0a6833b6f701c8c8febadfe2f45e25df29493602> > )? > > Thanks, have a nice day. >-- 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.
Mohamed Wael Khobalatte
2013-Jul-19 20:31 UTC
Re: Why last doesn''t return an ActiveRecord::Relation
Gotcha. In this case, since calls to Model.last(n) is inherently inefficient (loading everything and placing a limit), it''s good to keep it till the end of the chain. On Fri, Jul 19, 2013 at 11:55 AM, Tejas Dinkar <tejasdinkar@gmail.com>wrote:> On Jul-19-2013, at 8:08 PM, Mohamed Wael Khobalatte < > wael.khobalatte@gmail.com> wrote: > > Because you won''t need to run a query on one element? > > > I think the question is more about Model.last(5) or Model.first(5) > > These return arrays when they could return a relation. > -- > Tejas Dinkar > http://www.nilenso.com > Nilenso Software (formerly C42 Engineering) > > -- > 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. > > >-- Mohamed Wael Khobalatte -- 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.
Daniel Evans
2013-Jul-19 20:35 UTC
Re: Re: Why last doesn''t return an ActiveRecord::Relation
"last(5)" is, with regard to efficiency, equivalent to "limit(5).to_a". On Fri, Jul 19, 2013 at 2:28 PM, Andrew Vit <andrew@avit.ca> wrote:> "first" and "last" have always returned an instance of the model. These > are meant to be the final method that you call on a relation, to get the > result. If you call "last(5)" it will return an array, and that''s the > correct behaviour since that''s the result of the query. If you need the > relation so you can add other criteria on top of it, you can still use > "limit(5).order(arel_table[:id].desc)" > > Andrew Vit > > > > On Wednesday, July 17, 2013 8:33:52 AM UTC-7, Robin Dupret wrote: >> >> Hello, >> >> Sorry if this has been still answered, I haven''t found nothing on it. I >> would love to know why ActiveRecord::Base#last doesn''t return an >> ActiveRecord::Relation just like all or where since an >> ActiveRecord::Relation can act more or less like an array (as specified >> here<https://github.com/rails/rails/commit/0a6833b6f701c8c8febadfe2f45e25df29493602> >> )? >> >> Thanks, have a nice day. >> > -- > 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.