ruby on rails - passing hash object values as search parameter -
i have table "email" field "contents" data stored hash object. want search in table parameter "email => a5his@gmail.com" :email key , "a5his@gmail.com" value of hash object.
thanks in advance
this search going extremely inefficient. if plan on doing lot of of searches email, might prudent add indexed column store email in table.
i assuming contents
text field store serialized hash. hashes serialized using yaml format, in plain text. can perform regular wild card searches on content
column.
eg: hash shown below
{"email" => "a5his@gmail.com"}
is serialized in string
"--- \nemail: a5his@gmail.com\n"
so can write simple matcher follows:
email.all(:conditions => ["content ? ", "%email: #{email}%"])
if planning on querying several dynamic fields then,
class email < activerecord::base def self.find_all_by_dynamic(hash) ca = hash.map {|k, v| ["contents ?", "%#{k}: #{v}%"]}.transpose email.all(:conditions => [ca[0].join(" , "), *ca[1]]) end end
now can use new method as:
email.find_all_by_dynamic(:email => "5his@gmail.com", :zip => 94307)
Comments
Post a Comment