views:

37

answers:

1

Hi Everyone,

I have a working Rails application on version 2.3.5 - I am using many to many model relations and have got almost everything working.

What I would like to do is on my new kase page show the most recent kase job ref at the top.

So for example, if I created a new kase with a job ref of "001", if I then went to create a new kase it would show at the top "Your previous kases reference was 001".

I have the field of jobref in the new kase form, so I am trying to workout what I need to do to output only the last jobref.

If that makes sense!

Thanks,

Danny

EDIT: kase model.

class Kase < ActiveRecord::Base
  def self.all_latest
      find(:all, :order => 'created_at DESC', :limit => 5)
  end

  belongs_to :company # foreign key: company_id
  belongs_to :person # foreign key in join table

  # Paperclip
  has_attached_file :avatar

  # SEARCH FACILITY
  def self.search(search)
    search_condition = "%" + search + "%"
    find(:all, :conditions => ['jobno LIKE ? OR casesubject LIKE ? OR transport LIKE ? OR goods LIKE ? OR comments LIKE ? OR invoicenumber LIKE ? OR netamount LIKE ? OR clientref LIKE ? OR kase_status LIKE ? OR lyingatlocationaddresscity LIKE ?', search_condition, search_condition, search_condition, search_condition, search_condition, search_condition, search_condition, search_condition, search_condition, search_condition])
  end

end
+1  A: 

Kase.first(:order => 'id DESC')

Or if you have a created_at column:

Kase.first(:order => 'created_at DESC')

There are many different ways to get what you want, and most depend on how your model is defined.

Edit:

Model:

def self.most_recent
  first(:order => 'id DESC') # or whatever query you need to get the most recent
end

View:

<% if Kase.most_recent %>
  <p>Your previous kases reference was <%= Kase.most_recent.jobref %>.</p>
<% end %>
Tony Fontenot
How would I display by adding this code to only the new.html.erb, or would it have to go in the kase model?I have added my kase model to my question above.
dannymcc
see my update ...
Tony Fontenot
Great, that worked a treat. Thanks!
dannymcc