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

The Archive Base Latest Questions

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

params[:codes] = 9,10 @result = Candidate.find :all, :joins => params[:codes].split(‘,’).collect {|c| [INNER JOIN candidates_codes

  • 0
 params[:codes] = "9,10"
    @result = Candidate.find :all,
         :joins =>
           params[:codes].split(',').collect {|c| ["INNER JOIN candidates_codes on candidates_codes.candidate_id = candidates.id, INNER JOIN codes on codes.code_id = candidates_codes.code_id AND codes.value = ?", c]}

Error

Association named 'INNER JOIN candidates_codes on candidates_codes.candidate_id = candidates.id, INNER JOIN codes on codes.code_id = candidates_codes.code_id AND codes.value = ?' was not found; perhaps you misspelled it?

Update

CREATE TABLE `candidates` (
 `id` int(11) NOT NULL auto_increment,
`first_name` varchar(255) collate utf8_unicode_ci default NULL,
`last_name` varchar(255) collate utf8_unicode_ci default NULL,
`mobile_number` varchar(255) collate utf8_unicode_ci default NULL,
`address` text collate utf8_unicode_ci,
`country` varchar(255) collate utf8_unicode_ci default NULL,
`created_at` datetime default NULL,
`updated_at` datetime default NULL,
PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;

  CREATE TABLE `candidates_codes` (
   `candidate_id` int(11) default NULL,
   `code_id` int(11) default NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `codes` (
 `id` int(11) NOT NULL auto_increment,
 `section` varchar(255) collate utf8_unicode_ci default NULL,
 `value` varchar(255) collate utf8_unicode_ci default NULL,
 `created_at` datetime default NULL,
 `updated_at` datetime default NULL,
 PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=11 ;

Hi,

I am trying to create a find which can ether be “OR” or “AND”

for example(pseudocode)

array a = (1,2)
array b = (1)

find(1 AND 2) = array a
find(1 OR 2) = array a, array b 

My code currently looks like this –

@result = Code.all :joins => :candidates,
        :conditions => ["codes.id IN (?)", params['searches'][:Company]],
        :select => "candidates.*"

code is a table full of codes that describe a candidate,
a habtm relationship exists between code and candidate

The only way of using AND I can see in the guides is between two columns..

Many Thanks

Alex

  • 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:28:52+00:00Added an answer on May 15, 2026 at 1:28 pm

    Since the association is done with a join table, doing an AND requires an INNER JOIN, once for each term in the AND. What you’re trying to do is find a given candidate that has a mapping for all of the codes.

    This could get messy, since you not only have to join for each term, but also again to the codes table if you’re matching on a field there, such as value.

    Assuming the number of terms isn’t too high, and you pass in params[:codes] = “1,5,9”, and that you’re trying to match on codes.value:

    Candidate.find :all,
        :joins =>
          params[:codes].split(',').collect {|c| "INNER JOIN candidates_codes#{c} on candidates_codes#{c}.candidate_id = candidates.id INNER JOIN codes#{c} on codes#{c}.id = candidates_codes#{c}.code_id AND codes#{c}.value = c"}
    

    …or something like that. Warning that I haven’t tested that code, but give it a whirl if that’s what you’re looking for.

    Note I’ve removed the substitution from the last rev (where the ? is replaced by a variable) because joins don’t support this. You should first sanitize the params (i.e. make sure they are integers, or whatever), or use the protected sanitize_sql method in the model.

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

Sidebar

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.