60989

Using embeds on a child association to improve per

Question:

I have a contact and a lead. They are completely two independent models. I have a leads page where I can query against leads, sort by leads and do other work on leads. Also, a contact can be created without a lead. However, a contact can has_one lead. So I set up my models like so:

class Contact include Mongoid::Document has_one :lead field :reference_number field :some_date end class Lead include Mongoid::Document belongs_to :contact field :status end

Here is the problem. On the leads page, I want to sort leads by the reference_number which is an attribute of the contact. Because Contact and Lead are independent models, I cannot embed a lead into a contact, because I need to query leads on their own. So embed_one is NOT a solution. The other option I first tried was sorting using ruby, but with 10,000+ documents, this was extremely slow and quickly faded as a solution:

Lead.order_by( id: all.sort_by { |lead| lead.contact.reference_number }.map(&:id) )

So the only remaining solution is using some type of cache. First, I tried using a hash field like this:

class Lead ... field :denormalized_fields, type: Hash, default: {}

But this is not good either, because I still cannot sort by key/value pairs in a hash. This would not work:

Lead.order_by(denormalized_fields['reference_number'] => 'asc')

So I only have one remaining idea, but I am not sure if this is a good solution either. I introduce a new document called ParentAttribute:

class ParentAttribute include Mongoid::Document field :reference_number end class Contact include Mongoid::Document has_one :parent_attribute has_one :lead end class Lead include Mongoid::Document belongs_to :contact embeds_one :parent_attribute end

Now when I create a contact, rather than storing fields in Contact, I store it in ParentAttribute. And a Contact will still has_one lead, but I can also query against the attributes in Lead:

Lead.order_by(:'parent_attributes.reference_number' => 'asc')

I have no other ideas. Can I get some insights into my issue here?

Recommend

  • Converting matlab code, into c
  • i18n yellow screen of death
  • Restore files deleted with git clean -df
  • Simple DB query on Google App Engine taking a lot of CPU time
  • Substrings and if statements in Java [duplicate]
  • Is there any way to override the -fvisibility=hidden at link time?
  • Export from Excel to a word Document and insert a header in word
  • Which MySQL collation compares e.g. é and e as equal?
  • Numpy - Finding matches across multiple co-ordinates
  • import XML to HTML
  • Creating a Countdown Watch with a slider in Xcode
  • getting cpu usage of one process using c++ (win32)
  • Is a NULL pointer's dereference also equals NULL?
  • ruby on rails TypeError in Users#show - Cannot visit Like
  • Shortest route between multiple points in mapkit for iphone app
  • Embedded or referenced relations
  • Restore session after restart in JBoss
  • Botframework - resize bot chat in width [duplicate]
  • maven-shade-plugin reports: Error creating shaded jar: …target/classes (Is a directory)
  • Create ranking for vector of double
  • opengl: adding higher resolution mipmaps to a texture
  • Rails CarrierWave versions are not created for some reason
  • Weird session behaviour in codeigniter
  • Why are views not counted if you embed a youtube iframe dynamically using jquery?
  • How to get the click event on sIFR embedded tag in IE?
  • Getting IIS6 to play nice with WordPress Pretty Permalinks
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • HTML download movie download link
  • Running a C# exe file
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • How to include full .NET prerequisite for Wix Burn installer
  • KeystoneJS: Relationships in Admin UI not updating
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Hits per day in Google Big Query
  • Checking variable from a different class in C#
  • Converting MP3 duration time