从连接到远程mysql数据库的模型执行的其他查询

时间:2013-12-09 12:01:07

标签: mysql sql ruby-on-rails ruby-on-rails-3 activerecord

每当我调用模型中的任何方法时,我都会看到执行以下四个额外查询。型号名称为City。我使用connection_ninja(https://github.com/cherring/connection_ninja

连接到远程mysql遗留数据库
SET SQL_AUTO_IS_NULL=0, NAMES 'utf8', @@wait_timeout = 2147483   
SHOW FULL FIELDS FROM `City` 
SHOW TABLES LIKE 'City'  
SHOW CREATE TABLE `City` 

我模型的代码如下:

class CityRlOp < ActiveRecord::Base
  @conn = use_connection_ninja(:rl_op)
  self.table_name = 'City'

  def self.get_all_city_names
    city_names = Rails.cache.read "geomapping_city_names"
    if city_names.nil?
      city_names = []
      select("distinct city_name").each do |name|
        city_names.push name
      end
      Rails.cache.write "geomapping_city_names", city_names
    end
    city_names
  end

  def self.city_name_exists?(name)
    records = where(city_name: name)
    if records.count > 0
      city_names = Rails.cache.read "geomapping_city_names"
      city_names.push name
      Rails.cache.write "geomapping_city_names", city_names
      true
    else
      false
    end
  end
end

每次点击模型时都需要这些查询吗?

请帮助 谢谢

0 个答案:

没有答案