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 8369785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:44:31+00:00 2026-06-09T13:44:31+00:00

Contract has_many Invoices. After creating a contract, the model automatically creates an invoice depending

  • 0

Contract has_many Invoices. After creating a contract, the model automatically creates an invoice depending on certain form params.

I’m wanting to be able to select the most recently created invoice from a given contract.

This what I’m wanting to achieve:

contract = Contract.new(params)
contract.save

invoice = contract.invoices.most_recent  # < wanting to get the most recent

#or possible even do this if it's quicker
invoice = contract.most_recent_invoice

Which method is most proficient and what’s the best way to go about doing this? Does Rails have a built in method? I just want to learn and maintain best practices.

Thanks!

  • 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-09T13:44:33+00:00Added an answer on June 9, 2026 at 1:44 pm

    I would add a class method to the Invoice model that orders them by created_at and returns the first one:

    class Invoice < ActiveRecord::Base
      def self.most_recent
        order('created_at DESC').limit(1).first
      end
    end
    
    invoice = contract.invoices.most_recent # => Gives the last invoice created for this contract
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a script to create a new Invoice record. I'd like to automatically
I have a model, Market that has a one-to-many relation to another model, Contract:
A contact has_many notes; notes belong_to a contact. In my notes controller, after a
I have 3 models: #Client.rb has_many :contracts has_many :accessories, :through => :contracts #Contract.rb has_and_belongs_to_many
My main entity is called [Contract]. Contract has a many 2 many relationship w/
I have a wcf sevice and silverlight client. My contract has callbacks and therefore
I'm dynamically sub classing a generic type (filling it's contract) that has a generic
I have a model called Contact which has_one guest like so. class Contact <
I have two domain classes: Contract and Orgainisation . A contract has one contractor
I have a WCF contract with circular references. For a simple parent-child relationship, the

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.