分享

1.Rails 开发环境日志过大时自动清理

新建文件:config/initializers/clear_logs.rb

例:开发模式下当日志大于10m时自动清理

# config/initializers/clear_logs.rb
if Rails.env.development?
  require 'find'
  MAX_LOG_SIZE = 10.megabytes
  # logs = [ File.join(Rails.root, 'log', 'development.log'), File.join(Rails.root, 'log', 'test.log') ]
  logs = Find.find("#{Rails.root}/log")
  logs.each do |log|
    next if log = "#{Rails.root}/log/production_log"
    if File.size?(log).to_i > MAX_LOG_SIZE
      $stdout.puts "Removing Log: #{log}"
      `rm #{log}`
    end
  end
end

2.save失败的情况

before_create :default_value_for_create

def default_value_for_create
    self.boolean = false
end

这段代码调用save会报错

最后一行,因为ruby会自动返回方法的最后一行做为结果,所以此方法返回false,最终导致save对象时出错,改法也很简单,在方法最后添加一行true即可。

修改后的代码

before_create :default_value_for_create

def default_value_for_create
    self.boolean = false
end

3.清理rails应用不必要的路由

经过几年的不断发展,您的 rails应用程序 变得越来越大,并且进行一些清理是个好习惯。最明显的清除方法之一是清除未使用的路由。
添加新的路由会直接使用 resources :objects 在 config/routes.rb 文件,而不是添加单个成员或集合路由,使用resources会在运行时创建不必要的路由

Rails.application.routes.draw do
  resources :users
end
resources生成的path

但是,可能我们未使用所有上述方法,或者仅需要 users#new, users#create 和 users#show 我们的Users控制器中的方法

class UsersController < ApplicationController
  def new
  end

  def create
  end

  def show
  end
end

4.清理不必要的路由脚本

Rails.application.eager_load!
unused_routes = {}

# Iterating over all non-empty routes from RouteSet
Rails.application.routes.routes.map(&:requirements).reject(&:empty?).each do |route|
  name = route[:controller].camelcase
  next if name.start_with?("Rails")

  controller = "#{name}Controller"

  if Object.const_defined?(controller) && !controller.constantize.new.respond_to?(route[:action])
    # Get route for which associated action is not present and add it in final results
    unless Dir.glob(Rails.root.join("app", "views", name.downcase, "#{route[:action]}.*")).any?
      unused_routes[controller] = [] if unused_routes[controller].nil?
      unused_routes[controller] << route[:action]
    end
  end
end

puts unused_routes 
# {"UsersController"=>["edit", "update", "update", "destroy"]}

5.Time相关

使用当前机器作业系统的时区

# 今天
Time.now.to_date
Date.today

# 昨天
Time.now.yesterday.to_date
Date.today.yesterday

# 指定時間
Time.at(1520015100).to_date
Time.parse('2018-03-03 02:25:00').to_date

使用Rails 中设定的时区

# 今天
Time.current.to_date
Time.zone.now
Date.current

# 昨天
Time.current.yesterday.to_date
Time.zone.yesterday
Date.current.yesterday
Date.yesterday

# 指定時間
Time.zone.at(1520015100).to_date
Time.zone.parse('2018-03-03 02:25:00').to_date

指定时区

# 今天
Time.now.in_time_zone('Japan').to_date
Time.find_zone('Japan').today

# 昨天
Time.now.yesterday.in_time_zone('Japan').to_date
Time.find_zone('Japan').yesterday

# 指定時間
Time.find_zone('Japan').at(1520015100).to_date
Time.find_zone('Japan').parse('2018-03-03 02:25:00').to_date
Time.parse('2018-03-03 02:25:00 +0900').to_date

6.elasticsearch相关

Elasticsearch默认fields1000报错解决 #3

解决方案:
说明:对于已经建立的索引可以通过设置fields进行修复,对于之后的将建立的索引通过设置template进行设置
yourEShost:port本地开发一般是 localhost:9200

对于已经建立的索引

curl -XPUT yourEShost:port/your_index_name/_settings -d '{"index.mapping.total_fields.limit": 0}'
#以上表示对于‘your_index_name’这个索引设置fields为无限制,默认为1000
curl -XPUT yourEShost:port/*/_settings -d '{"index.mapping.total_fields.limit": 50000}'
#这个表示对所有index的fields的limit设置为50000
curl yourEShost:port/_cat/indices/*?pretty
#查看所有索引,查看指定索引将*换为索引名称即可
curl -XGET yourEShost:port/_all/_settings?pretty
#查看所有索引的设置
curl yourEShost:port/bilogs-logics-202.2017.11.21/_settings?pretty
#查看单个索引的设置
curl yourEShost:port/bilogs-logics-202.2017.11.23/_mapping?pretty
#查看单个索引的map

对于未创建的索引,可以通过模板设置

curl -XPUT 'yourEShost:port/_template/all ' -d '
{
  "template": "*",
  "settings": {
    "index.mapping.total_fields.limit": 50000,
    "refresh_interval": "30s"
  }
}'
#数据量比较大,数字建议设置大一点
#设置template的setting,
curl -XGET yourEShost:port/_template/*?pretty
#查看所有模板的设置,使用了*匹配,如果看指定的模板将*换成对应模板名即可,另外这里可以看到每个模本都有一个"order"字段,这个字段的数值越低,优先级越高,优先级高的模板会覆盖优先级低的模板

elasticsearch删除索引

删除索引my_index

curl -XDELETE http://localhost:9200/my_index

删除所有索引

curl -XDELETE http://localhost:9200/_all
 或 curl -XDELETE http://localhost:9200/*
elasticsearch 某些字段不存入索引
def search_data
   attributes.except('xxxxx', 'xxxxx')
 end
elasticsearch使用scope
  scope :search_import, -> { where(xxxxxx: true) }

指定index
    conditions = {
      language: "chinese",
      misspellings: false,
      highlight: {tag: "<span class='red'>"},
      fields: [:xxxx, :xxxxx],
      load: false,
      index_name: [Model, Model, Model, Model],
      page: params[:page],
      per_page: 20
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容