Devin
Devin

Reputation: 1017

rails search multiple attributes of multiple modules

I'm trying to search all columns in my Ticket model. Most columns are assocaited with a belongs_to association so I have to search the attributes of the info in my columns because people won't know the IDs.

These articles helped: Search multiple db columns in Rails 3.0

Rails: Search in has_one association

So far I have the below code, but it only searches the top param. How can I get it so that if it doesn't find anything in the top search, it tries the next. I'll be adding more to this.

Thanks

def self.search(search)
  if search
    Ticket.joins(:submitter).where('first_name LIKE ?', "%#{search}%")
  elsif  
    Ticket.joins(:issue).where('name LIKE ?', "%#{search}%")
  else
    all
  end
end

I figured it out, updated correct code here:

def self.search(search)
case search 
when /^[-+]?[0-9]*\.?[0-9]+$/
  Ticket.find(:all, :conditions => ['id LIKE :search', {:search => "%#{search}%"}])
else
  Ticket.joins(:submitter,:issue).find(:all, :conditions => ['name LIKE :search OR first_name LIKE :search', {:search => "%#{search}%"}])
end
end

Upvotes: 0

Views: 132

Answers (1)

Devin
Devin

Reputation: 1017

Updated code above - answer found

Upvotes: 1

Related Questions