This search is going to be extremely inefficient. If you plan on doing lot of osearches for email, it might be prudent to add an indexed column to store the email in your table.
I am assuming contents
is a text field to store a serialized Hash. The hashes are serialized using YAML format, which is in plain text. So you can perform the regular wild card searches on the content
column.
Eg:
A hash as shown below
{"email" => "[email protected]"}
is serialized in to string
"--- \nemail: [email protected]\n"
So you can write a simple matcher as follows:t
Email.all(:conditions => ["content LIKE ? ", "%email: #{email}%"])
If you are planning on querying several dynamic fields then,
class Email < ActiveRecord::Base
def self.find_all_by_dynamic(hash)
ca = hash.map {|k, v| ["contents LIKE ?", "%#{k}: #{v}%"]}.transpose
Email.all(:conditions => [ca[0].join(" AND "), *ca[1]])
end
end
Now you can use the new method as:
Email.find_all_by_dynamic(:email => "[email protected]", :zip => 94307)