Question about Recipe 52

I’m reading Rails Recipes, for the precision the recipe 52 that speak
about ‘Making your own Rails plugin’. The question, so stupid, is that i
don’t understand wath do the code

def self.search(query, fields, options = {})
find :all,
options.merge(:conditions => [[fields].flatten.map { |f|
“LOWER(#{f}) LIKE :query”}.join(’ OR ’ ),
{:query => “%#{query.to_s.downcase}%”}])
end

I think that:

  • query: is the text i search
  • fields: are the fields of Post in witch i search
  • options: ?

And then the command is a quite complicated for me, so i don’t
understand what it do. Thanks so much to all.

–Reis

Say you had a table, people, with fields: firstname, lastname, address,
suburb, city, etc.

Person.search ‘Nic’, [:firstname, :lastname]
will return results like:
Person(:firstname => ‘Nicholas’, :lastname => ‘Williams’, …)
Person(:firstname => ‘St. Nicholas’, :lastname => ‘Claus’, …)
Person(:firstname => ‘Reis’, :lastname => ‘Supernice’, …)

options are the rest of the find options you might like to include, say:
{:order => ‘lastname, firstname asc’}

Cheers
Nic

Dr Nic wrote:

Say you had a table, people, with fields: firstname, lastname, address,
suburb, city, etc.

Person.search ‘Nic’, [:firstname, :lastname]
will return results like:
Person(:firstname => ‘Nicholas’, :lastname => ‘Williams’, …)
Person(:firstname => ‘St. Nicholas’, :lastname => ‘Claus’, …)
Person(:firstname => ‘Reis’, :lastname => ‘Supernice’, …)

options are the rest of the find options you might like to include, say:
{:order => ‘lastname, firstname asc’}

Cheers
Nic

Thanks so much Dr Nic!
–Reis

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs