I have an app that tracks a net worth on a balance sheet for a client. There is a client model which has many balancesheets, a balancesheet model which belongs to a client, and each balancesheet has many assets and many liabilities (nested). On the show page for the balancesheet it lists the assets, liabilities, and a total net worth. Right now balancesheets are ordered by entering a quarter (1,2,3 or 4) and the year. So each client ends up having balancesheets listed as Q1 2010, Q2 2010 and so on.
What I want to do is to compare the asset, liability, and net worth totals on any given balancesheet show page with the previously entered balancesheet for that client. This could be from last quarter or last year, as long as it’s the previous entry.
How do I do this?
UPDATE Okay, so on a more simple model (my client model) I figured out the basics to show the next or previous client name in the list:
def nextname
self.class.
order( "id" ).
where( "id > :id", attributes.symbolize_keys).first
end
The only problem with this is when it gets to the last client in the table, it returns the error:
undefined method `name’ for nil:NilClass
Extracted source (around line #37):
37: <%= @client.nextname.name %>
What do I need to write in order to return something blank where there isn’t another record?
You should always be weary of calling methods on things that may not be defined. This has always been a problem that has suggested many solutions, each of which has its various merits.
In this case what you should do is write a helper method to avoid complicating your view, and make a model method that is more specific in what it does.
In your helper you would create a method like this:
In your case you would use it like this: