Ruby on Rails Sunday, February 1, 2015

Dear everyone,

I'd like to ask two questions, how can I optimize my performance of my current model?

I'd like to use the information from translations table as it is (all columns) ....

 => Translation(id: integer, word_id: integer, conversion_id: integer, created_at: datetime, updated_at: datetime, user_id: string, unit_id: integer, last_learned: date, missed: integer, learned: integer) 

... and merge it to any translations/conversions children of Word.

My model getter self_with_translations should attach it to all translations/conversions but using join.

Second thing would be to optimize the check for in unit_ids section for only select unit_ids once and not have many selects.

The program is right here: http://open-voc.org

I appreciate any help,

kind regards, 
Alex

Code below:
class Word < ActiveRecord::Base

  has_and_belongs_to_many :conversions, class_name: "Word",
                          join_table: "translations",
                          association_foreign_key: "conversion_id"

  def available_units=(unit_ids_as_array)
    @available_units = unit_ids_as_array
  end

  def available_units
    @available_units || []
  end

  def with_enriched_data
    #@FIXME join merge instead of calling sep.
    meta = Translation.where(word_id: self.id).first
    # fix for deleted units, don't deliver them!
    self.available_units = Unit.all.map{|u| u.id } # @FIXME Cache or call only one time
    unit_id = meta.unit_id if self.available_units.include? meta.unit_id
    # enrich with attrs unit_id, ..........
    attributes.merge(unit_id:  unit_id, missed: meta.missed)
  end

  def self_with_translations(targetlang_id={})
    conversions = self.conversions.where(language_id: targetlang_id).map do |c|
      c.with_enriched_data
    end
    attributes.merge(translations: conversions)
  end

end

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4ecdc95d-e483-4589-8656-44254be5b0a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment