How to search in this activerecord example?

Posted by Horace Ho on Stack Overflow See other posts from Stack Overflow or by Horace Ho
Published on 2010-05-28T01:57:06Z Indexed on 2010/05/28 2:01 UTC
Read the original article Hit count: 237

Filed under:
|

Two models:

Invoice

  :invoice_num        string
  :date               datetime
  .
  .
  :disclaimer_num     integer (foreign key)

Disclaimer

  :disclaimer_num     integer
  :version            integer
  :body               text

For each disclaimer there are multiple versions and will be kept in database. This is how I write the search (simplified):

scope = Invoice.scoped({ :joins => [:disclaimer] })
scope = scope.scoped :conditions => ["Invoice.invoice_num = ?", "#{params[:num]}"]
scope = scope.scoped :conditions => ["Disclaimer.body LIKE ?", "%#{params[:text]}%"]

However, the above search will search again all versions of the disclaimer. How can I limit the search to only the last disclaimer (i.e. the version integer is the maximum).

Please note:

Invoice does not keep the version number. New disclaimers will be added to disclaimer table and keep old versions.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord