views:

53

answers:

3

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 ]
      )
A: 

Try this out.

Assuming that you have mode of History

History.find(:all ,
    :conditions=>[" institution_id IN (? ) ",params['searches'][key] ],
    :group => ["candidate_id  HAVING count(*) = ? " ,params['searches'][key].size ]
)
Dinesh Atoliya
I think the author is looking to get information on the candidates which would require some sort of join. I think we need some sort of additional information on the schema and relationships between the models before providing a complete answer.
Brian
I tried that, and I'm getting " you have an error in your sql syntax '? 1' at line 1: SELECT * FROM `histories` WHERE ( institution_id IN ('1' ) ) GROUP BY candidate_id HAVING count(*) = ? 1
Josh Crowder
+1  A: 

Give this a try (building on Dinesh's approach above:

candidates = History.find(:all,
  :joins => :candidates,
  :select => "candidates.*"
  :conditions => ["institution_id IN (?)", params['searches'][key]],
  :group => ["candidate_id HAVING count(*) = ?", params['searches'][key].size]
)

Warning - untested.

Brian
I tried that, but I get uninitialized constant History::Candidates
Josh Crowder
Oops, noticed something... In you history model, belongs_to :candidates should be belongs_to :candidate (not plural). You also may run into issues w/ the has many => through in the Candidate model.
Brian
I dont know if this will make any difference but I am in the Searches Controller.
Josh Crowder
This will give a syntax error, as the `:group` parameter does not accept `conditions` array. It can only be a string OR a symbol.
KandadaBoggu
+1  A: 

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
KandadaBoggu
Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?1' at line 1: SELECT candidates.* FROM `histories` INNER JOIN `candidates` ON `candidates`.id = `histories`.candidate_id WHERE (institution_id IN ('1')) GROUP BY candidate_id HAVING count(*) = ?1
Josh Crowder
I have updated my answer as there was a syntax error. The SQL you have listed DOES not correspond to the SQL generated by the solution I have suggested.
KandadaBoggu
Awesome thank you very much that worked perfectly, although I had to change > 3 to >=
Josh Crowder