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

但是,可能我们未使用所有上述方法,或者仅需要 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
}
网友评论