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!
I would add a class method to the Invoice model that orders them by created_at and returns the first one: