views:

79

answers:

3

I have a named scope (name) combination of first and last name and I'm wanting to use this in a search box.

I have the code below:

named_scope :full_name, lambda { |fn| {:joins => :actor, :conditions => ['first_name LIKE ? OR second_name LIKE ?', "%#{fn}%", "%#{fn}%"]} }

def self.search(search)
  if search
    self.find(:all, :conditions => [ 'full_name LIKE ?', "%#{search}%"])
  else
    find(:all)
  end
end

but this doesn't work as it gives the following error:

SQLite3::SQLException: no such column: full_name: SELECT * FROM "actors" WHERE (full_name LIKE '%eli dooley%') 

Thanks in advance

Houlahan

+2  A: 

Try this:

def self.search(search)
  if search
    self.full_name(search)
  else
    find(:all)
  end
end

The named scope does not add a column to the database, but an easily accessible way to get records without typing conditions every time.

Tony Fontenot
+1  A: 
John Topley
i tried using the self.full_name(search) but now i get an error when trying to display all the actors: Association named 'actor' was not found; perhaps you misspelled it? the actor is used in the loop when displaying all the actors in the index page.
That's really a separate question. You need to post the association code within the model class that contains this named scope.
John Topley
A: 

Have you looked at Searchlogic?

I highly recommend it.

http://github.com/binarylogic/searchlogic

Danger Angell