views:

70

answers:

1

Hi Everyone,

I have a kase model which I am using a simple search form in. The problem I am having is some kases are linked to companies through a company model, and people through a people model.

At the moment my search (in Kase model) looks like this:

# SEARCH FACILITY
  def self.search(search)
    search_condition = "%" + search + "%"
    find(:all, :conditions => ['jobno LIKE ? OR casesubject LIKE ? OR transport LIKE ? OR goods LIKE ? OR comments LIKE ? OR invoicenumber LIKE ? OR netamount LIKE ? OR clientref LIKE ? OR kase_status LIKE ? OR lyingatlocationaddresscity LIKE ?', search_condition, search_condition, search_condition, search_condition, search_condition, search_condition, search_condition, search_condition, search_condition, search_condition])
  end

What I am trying to work out, is what condition can I add to allow a search by Company or Person to show the cases they are linked to.

@kase.company.companyname

and

company.companyname

don't work :(

Is this possible?

Thanks,

Danny

EDIT:

find(:all, :conditions => ["kase.jobno LIKE :q OR kase.casesubject LIKE :q OR kase.transport LIKE :q OR company.companyname LIKE :q OR person.personname LIKE :q", {:q => search_condition}])

Like that?

EDIT 2:

kase model:

class Kase < ActiveRecord::Base
  belongs_to :company # foreign key: company_id
  belongs_to :person # foreign key in join table
  belongs_to :surveyor,
             :class_name => "Company",
             :foreign_key => "appointedsurveyor_id"
  belongs_to :surveyorperson,
             :class_name => "Person",
             :foreign_key => "surveyorperson_id"

kase controller:

 # SEARCH FACILITY
  def self.search(search)
    search_condition = "%" + search + "%"
    find(:all, :conditions => ['jobno LIKE ? OR casesubject LIKE ? OR transport LIKE ? OR goods LIKE ? OR comments LIKE ? OR invoicenumber LIKE ? OR netamount LIKE ? OR clientref LIKE ? OR kase_status LIKE ? OR lyingatlocationaddresscity LIKE ?', search_condition, search_condition, search_condition, search_condition, search_condition, search_condition, search_condition, search_condition, search_condition, search_condition])
  # find(:all, :conditions => ["kases.jobno LIKE :q OR kases.casesubject LIKE :q OR kases.transport LIKE :q OR kases.goods LIKE :q OR kases.comments LIKE :q OR kases.clientref LIKE :q OR kases.kase_status LIKE :q OR kases.lyingatlocationaddresscity LIKE :q OR companies.companyname LIKE :q OR people.personname LIKE :q", {:q => search_condition}], :join => [:person, :company])
  end
+1  A: 

You probably want to use the :joins => [:company, :person] option and then rewrite the :conditions to 'kase.jobno LIKE ? ... company.companyname LIKE ? ...'.

So:

find(:all, :conditions => ["kases.jobno LIKE :q OR kases.casesubject LIKE :q OR kases.transport LIKE :q OR companies.companyname LIKE :q OR people.personname LIKE :q", {:q => search_condition}], :joins => [:person, :company])
Jakub Hampl
So taking both comments into account, I have updated my question - is that right?
dannymcc
Don't forget to add the `:join`. So it should be like this: `find(:all, :conditions => ["kase.jobno LIKE :q OR kase.casesubject LIKE :q OR kase.transport LIKE :q OR company.companyname LIKE :q OR person.personname LIKE :q", {:q => search_condition}], :join => [:person, :company])`.
Jakub Hampl
Also the dot notation uses the table names which by ruby convention are pluralized. So it should be `find(:all, :conditions => ["kases.jobno LIKE :q OR kases.casesubject LIKE :q OR kases.transport LIKE :q OR companies.companyname LIKE :q OR people.personname LIKE :q", {:q => search_condition}], :join => [:person, :company])`
Jakub Hampl
Hmm, I added and midified the above to include all the fields I wanted but get the following error:ArgumentError in KasesController#searchUnknown key(s): join
dannymcc
Could you post the updated code then?
Jakub Hampl
Sure, see above.
dannymcc
Sorry stupid error: the proper name is not `join` but `joins`.
Jakub Hampl
You are a genius! Thanks for that, I couldn't get it working no matter how much I tried!Thanks again!
dannymcc