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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:13:37+00:00 2026-05-15T13:13:37+00:00

Two models: Invoice :invoice_num string :date datetime . . :disclaimer_num integer (foreign key) Disclaimer

  • 0

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.

  • 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-05-15T13:13:38+00:00Added an answer on May 15, 2026 at 1:13 pm

    If you want only the invoices with the latest version from disclaimer, put a condition on the disclaimer_num. And I also suggest creating a helper method in Disclaimer to make the code cleaner in your scope.

    class Disclaimer < ActiveRecord::Base
      def latest
        find(:first, :order => "version DESC")
      end
    end
    
    scope = scope.scoped :conditions => { :disclaimer_num => Disclaimer.latest }
    

    And I really hope you removed the sql injection prevention code from your scope for brevity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When two models are related to each other, Doctrine automatically generates foreign key constraints
I have two models: public class ProfessorModels { public string FullName { get; set;
I have these two models class Invoice < ActiveRecord::Base has_many :items accepts_nested_attributes_for :items ...
I have two models: Enrollment and Invoice. enrollment belongs to invoice invoice has many
Two models: class Task < ActiveRecord::Base has_many :subtasks end class Subtask < ActiveRecord::Base belongs_to
I have two models, Listing and Invitation, associated with has_and_belongs_to_many. I am looking at
I've two models: The first model: class Url < ActiveRecord::Base attr_accessible :code, :target, :user_id,
I have two Models, Programme and Event, a programme has many events. I need
I have got two models, User and Event. I need two kind of relationship
I have two models: User and Car with the following associations: User has_many Car

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.