> On Mar 10, 2016, at 3:11 AM, Colin Law <clan...@gmail.com> wrote:
> 
> On 9 March 2016 at 22:25, David Williams <li...@ruby-forum.com> wrote:
>> Colin Law wrote in post #1182073:
>>> On 9 March 2016 at 21:51, David Williams <li...@ruby-forum.com> wrote:
>>>> "articles"."category_id" = ?  ORDER BY "articles"."created_at" DESC
>>>> [["category_id", 1]]
>>>> 
>>>> 
>>>> I'm getting undefined method `each' for #<Article:0x8300868>
>>> 
>>> That is because you only have a single Article, not a collection, see
>>> above.
>>> 
>>> Colin
>> 
>> Thanks for responding Colin, I didn't realize .last would lead to a
>> single article being queried. I thought it would give me the last 10
>> records. I guess DESC being my default named_scope: default_scope -> {
>> order(created_at: :desc) } should take care of that for me. Thanks.
> 
> The limit(10) gives you the first ten records, but since you have
> ordered descending that means you will get the ten with the highest
> created_at.  Although default_scope sounds like a good idea I have
> several times used it only to find that it causes confusion and now
> avoid it.  Preferring to explicitly specify the order whenever it
> matters (via named scopes if appropriate).

Part of one of the RailsConf sessions I went to, either last year or the year 
before, could have been re-titled "default_scope considered harmful". I don't 
think it's deprecated or anything, but it is definitely something to use 
sparingly, and with a lot of forethought about what other scopes you may need 
later. Better to chain scopes, so you call ControllerName.your_scope.latest, 
rather than adding the "latest" behavior into your default scope. It makes it 
more declarative. More (duplicated) characters is not really the issue you want 
to prioritize. Being able to reason about your code in the current bit you are 
working on is.

Walter

> 
> Colin
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to rubyonrails-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLva0CaJZaRcMJTW%3DH1kBvmkWCmQUzYhcdGJJP8YW-t97Q%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/AB69B600-DF5C-4373-BB74-17F6495E33E9%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to