开发者

Rails, search item in different model?

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_con开发者_Go百科dition, 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


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])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜