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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:24:22+00:00 2026-05-12T11:24:22+00:00

I’m hoping I’m doing something wrong here and someone can point me in the

  • 0

I’m hoping I’m doing something wrong here and someone can point me in the right direction…

I have written a method inside of one of my models, here is the code:

def self.find_by_user_id(user_id, *args)
    self.find(:all, :joins => :case_map,
       :conditions => ['case_maps.uer_id = ? and case_maps.case_id = cases.id', user_id], 
        *args)
end

I can call the code like this and it works as expected:

Case.find_by_user_id(some_user_id)

However, when this code is executed with any additional args, like this:

Case.find_by_user_id(some_user_id, :limit => 15)

I get back ALL cases. The query in my log file show that it executed this:

Case Load (0.6ms)   SELECT * FROM `cases` LIMIT 15

I even put a logger.info message in that method to make sure it’s the one that is executing… It seems that whenever *args is not nil, that it skips all of the conditions and joins I added to the find and just uses the *args.

Anyone see something that I’m doing wrong?

  • 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-12T11:24:22+00:00Added an answer on May 12, 2026 at 11:24 am

    AR::B#find expects a variable number of arguments, but the last of those should be the options hash. It uses Array#extract_options!, and so can you:

    def self.find_by_user_id(user_id, *args)
      options = args.extract_options!
      options.merge!(:joins => :case_map, :conditions => 
        ['case_maps.uer_id = ? and case_maps.case_id = cases.id', user_id])
      self.find(:all, *args, options)
    end
    

    But you shouldn’t. What possible values make sense in between the :all and the options fields?

    What you are really after is:

    def self.find_by_user_id(user_id, options)
      self.find(:all, options.merge(:joins => :case_map, :conditions => 
        ['case_maps.user_id = ? and case_maps.case_id = cases.id', user_id]))
    end
    

    Or one better:

    named_scope :find_by_user_id, lambda{|user_id|
      {
        :joins => :case_map,
        :conditions => ['case_maps.user_id = ? and case_maps.case_id = cases.id', user_id]
      }
    }
    

    The named scope will ensure all your options are cleanly merged (all your conditions are applied even if you add more later on). You can then call it with:

    Case.find_by_user_id(user_id).all(:limit => 15)
    

    (Named Scope are “Da Bom”. One should use them as much as possible. Even in general conversation. For example, if you were here in my house last night, you may have overheard this little ditty: “What do you want for your dinner?”, “Same thing I have every night, Named Scopes and chips.”. Because I’m committed and all that.)

    Also, as a side note, assuming this is on your Case model, the “and case_maps.case_id = cases.id” clause is unnecessary, :joins => :case_map does that for you.

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

Sidebar

Ask A Question

Stats

  • Questions 259k
  • Answers 259k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I ended up testing against ModelState["parent"].Value.AttemptedValue instead of the entity… May 13, 2026 at 11:09 am
  • Editorial Team
    Editorial Team added an answer USE NULLIF() UPDATE BinaryAssets.BinaryAssetStructures SET ParentBinaryAssetStructureId = NULLIF(@ParentBinaryAssetStructureId,-1) WHERE BinaryAssetStructureId… May 13, 2026 at 11:09 am
  • Editorial Team
    Editorial Team added an answer good answer by Nick Berardi - checking out Scott Gu's… May 13, 2026 at 11:09 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.