/app/models/spanish_word_for.rb
Ruby | 37 lines | 31 code | 3 blank | 3 comment | 2 complexity | 1be8b838d2855d3c50afbed5cd554164 MD5 | raw file
- class SpanishWordFor < ActiveRecord::Base
- # fields: english_word, spanish_word
- set_table_name 'spanish_word_for'
- set_primary_key 'id'
- def self.random_words(num, seed_id=500)
- ## filter using id, so that all records do not need to be searched and sorted by MySQL
- start_idx = seed_id - 500
- start_idx=0 if start_idx < 0
- end_idx = seed_id + 500
- end_idx = 800 if end_idx < 800
- conditions = "id > #{start_idx} AND id < #{end_idx}"
- SpanishWordFor.where(conditions).order('rand()').limit(num).all
- end
- ## iterates over all records and saves lookup_keys
- def self.generate_lookup_keys
- SpanishWordFor.where("lookup_key = ''").all.each do |word|
- lookup = word.english_word.gsub(/\(.*?\)/, '') # remove anything in parentheses
- lookup.gsub!(/'|!|\?/, '')
- lookup.gsub!(/ +/, ' ')
- word.lookup_key = SpanishDictionary.map_word(lookup)
- # split words at comma
- lookup_arr = lookup.split(",").collect do |lk|
- lk.strip!
- lk.gsub!(/ /, '-')
- SpanishDictionary.map_word(lk)
- end
- word.lookup_key = lookup_arr.join(",")
- word.save
- print word.lookup_key + "\n"
- end
- end
- end