Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8501461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:13:09+00:00 2026-06-11T01:13:09+00:00

I’ve never submitted anything to an open source project, and frankly, don’t care what

  • 0

I’ve never submitted anything to an open source project, and frankly, don’t care what the rails people think about the methods. I love the ‘last’ and ‘first’ methods and would like to extend that concept into the methods ‘second’-‘ninth’ for the purposes of sampling.

How might I create my own custom method that would graft these methods onto ActiveRecord?

**Currently watching Ryan Bates’s screencast #50 at railscasts.com

** brainstorming on how to implement this, I’m thinking it could loop over the :id values (0, 1, 2, 3, 4…) until it finds the next record. And then the ‘third’ command could skip past the first match until it finds the second match (Model.third), and so on. Ha now I’m wondering if .first uses this same method. I suppose I’ll check out the source code. 🙂

** As I venture down this road I’ll be leaving a list of relevant links (I can’t list more than 2 links unless I get 10 points. give me a one-up so I can add more)

http://railscasts.com/episodes/50-contributing-to-rails

http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:13:11+00:00Added an answer on June 11, 2026 at 1:13 am

    It’s actually a bit more complex than you might think, because first and last are not individual methods on individual classes, but they are implemented on many different classes in ActiveRecord.

    For example,

    • You can use Post.first, in which case you are calling ActiveRecord::Base#first.

    • You can use Post.where(:submitted => true).first, in which case you are calling ActiveRecord::Relation#first

    • You can use Post.comments.first, in which case you are calling ActiveRecord::Associations::CollectionAssociation#first

    See where this is going? In order to have similar behavior for second, third, and so on, you need to implement these methods in several places, not just one.

    But just to get you started, here’s how you could implement ActiveRecord::Relation#second:

    class ActiveRecord::Relation
      def second
        if loaded?
          @records[1]
        else
          @second ||= limit(2).to_a[1]
        end
      end
    end
    

    Execute this piece of code in an initializer, for example, and all of a sudden you call second on all relations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have much knowledge about the IPv6 protocol, so sorry if the question
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.