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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:52:31+00:00 2026-05-15T18:52:31+00:00

At the moment, I am doing my complex queries by hand so to speak.

  • 0

At the moment, I am doing my complex queries by hand so to speak. But I keep encountering problems. For instance.

          query = "SELECT histories.candidate_id 
          FROM histories 
          WHERE histories.institution_id IN (?) 
          GROUP BY histories.candidate_id 
          HAVING COUNT(*)= ?"
          cand = [Code.find_by_sql([query,

params[‘searches’][key],
params[‘searches’][key].size])]

class History < ActiveRecord::Base
  belongs_to :candidate
end
  create_table "histories", :force => true do |t|
    t.string   "job_title"
    t.date     "start_date"
    t.date     "finish_date"
    t.string   "basic_salary"
    t.string   "bonus"
    t.integer  "institution_id"
    t.integer  "candidate_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
class Candidate < ActiveRecord::Base
  # has_and_belongs_to_many :codes
  has_many :codes, :through => :CandidatesCodes
  has_many :histories
  has_many :contacts
  has_many :compensations
end

This returns a list of candidate ids.. but want I want it to return is a list of candidates how would I do that the rails way?

This is brians suggestion, and I have tried this but I get uninitialized constant History::Candidates

      cand = History.find(:all,
        :joins => :candidates,
        :select => "candidates.*",
        :conditions => [ "institution_id IN (?)", params['searches'][key] ],
        :group => [ "candidate_id HAVING count(*) = ?", params['searches'][key].size ]
      )
  • 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-15T18:52:32+00:00Added an answer on May 15, 2026 at 6:52 pm

    Try this:

    Candidate.all(
      :joins => :histories,
      :conditions => {:histories=> {:institution_id => params[:searches][key]}},
      :group => "candidates.id",
      :having => "count(candidates.id) >= %i" % params[:searches][key].size
    )
    

    This should generate the following SQL:

    SELECT   candidates.*
    FROM     candidates AS candidates
    JOIN     histories AS histories ON histories.candidate_id = candidates.id
    WHERE    histories.institution_id IN (1,2,3)
    GROUP BY candidates.id
    HAVING   COUNT(candidates.id) >= 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the moment I am doing this in each controller i need it :
I am doing a project at the moment and I need an efficient method
I'm doing javascript on a daily basis at the moment. I'm coming from an
So I am doing this as a learning moment and I'm not afraid to
HTTP Referer is the way I'm doing it at the moment. As everyone who's
I'm having an issue at the moment that I've been doing alot of searching
At the moment my code (PHP) has too many SQL queries in it. eg...
At the moment I'm doing this: gems(id, name, colour, level, effects, source) id is
At the moment I'm doing something like: public virtual String zipcodeRaw { get; set;
I'm doing this easy horizontal menu but I'm facing a funny issue... Dont know

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.