ruby on rails named scopes (searching)

I have a named scope (full_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
houlahanAsked:
Who is Participating?
 
JESiiCommented:
A few  questions/comments:
1) What is the select statement that's being attempted? (check in your development log)
2) Please provide the whole definition of your class
3) In general, isn't named_scope supposed to build the find for you... that would mean that you should be able to execute something like:
<pre>
  Actor.full_name(search)
</pre>
rather than the generic find that you are using. You are actually trying to put in two :condition clauses - one from the named_scope and one from the find method itself.
4) I don't understand the ":joins =>" in your lambda. If Actor is your sole model, then I don't think you need that... and I don't think you even need the lambda if that's the case I presume your full_name is "<first name> <last name>", therefore you need to split out the full_name into it's two parts to use the conditions statement. Therfore, I'd suggest (lacking more details) defining your named_scope  and your method something like this (assuming self = Actor):
Named Scope:
named_scope :full_name, lambda { |*args| { :conditions => ['first_name LIKE ? OR second_name LIKE ?', args.first, args.last] } }
Method:
def self.search(search)
  if search
    full_name(search.split(/ /)
  else
    find.all
  end
end

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.