On Oct 2, 9:57 pm, Rodrigo wrote:
I want to find all records in a model that contains the email
"j...@test.com" despite the fact that the email value is within an
array of hashes. How do I do this?

I have a table of email messages like so:

create_table :emails do |t|
t.string :emailMessageId
t.datetime :date
t.string :subject
t.string :gmailMessageId
t.string :gmailThreadId
t.string :from_hash, :default => nil
t.text :to_hash, :default => nil
t.text :cc_hash, :default => nil
t.integer :contact_id

The email.rb model file says:

class Email < ActiveRecord::Base

serialize :from_hash, Hash
serialize :to_hash, Array
serialize :cc_hash, Array

There are two major categories of solutions to this - either normalize
the data to more SQL records, or apply a full-text search system.
Michael's already described the SQL solution, so here's the other one.

You might want to consider using Sunspot (http://outoftime.github.com/
sunspot/ ) for this - it's got the ability to filter data by
multivalued string fields for exactly this sort of situation. The site
will provide you with setup information, but ultimately you'd end up
with a model like this:

class Email < ActiveRecord::Base
serialize :from_hash, Hash
serialize :to_hash, Array
serialize :cc_hash, Array

searchable do
string :to_emails, :multiple => true do
to_hash.map { |x| x['email'] }

Searching is pretty straightforward:

Email.search do
with :to_emails, 'joe@test.com'

This will also be a huge help when the inevitable, "Hey, can we search
email TEXT as well?" request comes in. :)

--Matt Jones

You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

Discussion Posts


Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 3 | next ›
Discussion Overview
grouprubyonrails-talk @
postedOct 3, '11 at 1:57a
activeOct 3, '11 at 9:31a



site design / logo © 2021 Grokbase