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). 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.