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

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.