Module: FuzzyMatch::CachedResult::ActiveRecordBaseExtension
- Defined in:
- lib/fuzzy_match/cached_result.rb
Instance Method Summary collapse
-
#cache_fuzzy_match_with(other_active_record_class, options) ⇒ Object
required options: :primary_key - what to call on this class :foreign_key - what to call on the other class.
Instance Method Details
#cache_fuzzy_match_with(other_active_record_class, options) ⇒ Object
required options: :primary_key - what to call on this class :foreign_key - what to call on the other class
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/fuzzy_match/cached_result.rb', line 32 def cache_fuzzy_match_with(other_active_record_class, ) other = other_active_record_class.to_s.singularize.camelcase me = name if me < other a = me b = other primary_key = :a foreign_key = :b else a = other b = me primary_key = :b foreign_key = :a end # def aircraft define_method other.underscore.pluralize do other.constantize.where [:foreign_key] => send("#{other.underscore.pluralize}_foreign_keys") end # def flight_segments_foreign_keys define_method "#{other.underscore.pluralize}_foreign_keys" do fz = ::FuzzyMatch::CachedResult.arel_table sql = fz.project(fz[foreign_key]).where(fz["#{primary_key}_class".to_sym].eq(self.class.name).and(fz["#{foreign_key}_class".to_sym].eq(other)).and(fz[primary_key].eq(send([:primary_key])))).to_sql connection.select_values sql end # def cache_aircraft! define_method "cache_#{other.underscore.pluralize}!" do other_class = other.constantize primary_key_value = send [:primary_key] other_class.fuzzy_match.find_all(primary_key_value).each do |other_instance| attrs = {} attrs[primary_key] = primary_key_value attrs["#{primary_key}_class"] = self.class.name attrs[foreign_key] = other_instance.send [:foreign_key] attrs["#{foreign_key}_class"] = other unless ::FuzzyMatch::CachedResult.exists? attrs ::FuzzyMatch::CachedResult.create! attrs end end end end |