分享

作者: 落叶05 | 来源:发表于2019-11-08 14:15 被阅读0次

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
    }

相关文章

  • 分享分享分享分享

    分享分享分享分享

  • 分享分享分享

    我向来都觉得,一个人不考虑家人的感受,置家人的期待和理想于不顾,而不是去沟通和体恤家人的话,不会是一个优秀和成熟的...

  • 分享分享分享

    阅读:富裕属于口袋里装满快乐的人 通往富裕以及抵达富裕后的路线图的第二步: ✅ 处于富裕的状态,是指内在状态,与外...

  • 分享

    分享分享分享分享分享分享分享分享分享分享分享分享分享

  • 【十】分享分享

    漫长等待牵挂的容颜, 在你回眸间重叠, 我丢失了语言, 再也无法说分别. 不想错过你所有时间, 却总有遗憾沉淀, ...

  • 分享的分享

    分享一位朋友的个人案例和交流收获,仅供参考: 这位男性朋友曾是深圳某500强公司的中高层干部,常年高负荷工作让他错...

  • 分享时刻<分享>

    分享一下很有意义的一段话如下 这段话不错,值得分享! 一群人能走到一起不容易,有的强势,有的随和,有的厉害,有的温...

  • 『分享』 今日分享

    1. 当你老了,回顾一生,就会发觉:什么时候出国读书,什么时候决定做第一份职业、何时选定了对象而恋爱、什么时候结婚...

  • 没写,分享分享

    电话打了几次才打通。 “每次你们的快递都是最快的,为什么这次这么慢?”女孩问道,“说好的空运当日就能收件,可这都过...

  • 分享分享吧

    1.通感 它虽然是比喻,要靠的是天马行空的想象力。比如《荷塘月色》里“微风过处,送来缕缕清香,仿佛远处高楼上渺茫的...

网友评论

      本文标题:分享

      本文链接:https://www.haomeiwen.com/subject/pcmibctx.html