琐事笔记

还是经常会忘记一些事情,毕竟是大脑内存容量有限啊

[b]一、rails[/b]
1、rails中 url 和 path的区别

def index
p users_path
p users_url
end

输出

"/users"
"http://127.0.0.1:3000/users"

一个是全路径,一个是相对路径
[quote]
*_path are for views because ahrefs are implicitly linked to the current URL. So it’d be a waste of bytes to repeat it over and over. In the controller, though, *_url is needed for redirect_to because the HTTP specification mandates that the Location: header in 3xx redirects is a complete URL.
[/quote]

2、解析路由

Rails.application.routes.recognize_path('/users/12')
=> {:action=>"show", :controller=>"users", :id=>"12"}


3、字符串去除html标签的

include ActionView::Helpers::SanitizeHelper


4、页面带block的帮助方法
页面帮助方法:
  
<% box_wrapper do %>
something
<% end %>

def box_wrapper(&block)
if block_given?
content = capture(&block)
content_tag(:div, content, :class => 'box')
end
end


5、rails3的批量操作

# 批量删除
delete_all

# 批量增加
activerecord-import implements AR#import
activerecord-import is a library for bulk inserting data using ActiveRecord.
see how it works:
menus = []
10.times { |i| menus << Menu.new(:name => "menu #{i}") }
Menu.import menus

# 批量查询
User.find_each(batch_size: 5000) do |user|
NewsLetter.weekly_deliver(user)
end



[b]二、ruby[/b]
1、保留两位小数
format("%.2f",1.222222).to_f  


2、注释
单行注释用井号即可

# 定义一个方法
def hello
# 打印hello
p "hello"
end


注释多行代码

=begin
def hello
p 'world'
end
=end


3.去除空格

# 去除一般的空格
> " sdfsd ".strip
=> "sdfsd"

# 带&nbsp的空格
> "010-123456     ".gsub(/[[:space:]]/,'')
=> "010-123456"





[b]三、mysql[/b]
1、mysql时间比对
select * from table_name where datediff('时间一','时间二') > 0

(>0 =0 <0 三种情况)

2、mysql启动操作
下操作方法:
[quote]一、启动方式
1、使用 service 启动:service mysqld start
2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start
3、使用 safe_mysqld 启动:safe_mysqld&
二、停止
1、使用 service 启动:service mysqld stop
2、使用 mysqld 脚本启动:/etc/inint.d/mysqld stop
3、mysqladmin shutdown
三、重启
1、使用 service 启动:service mysqld restart
2、使用 mysqld 脚本启动:/etc/inint.d/mysqld restart[/quote]

3、修改表的引擎
ALTER  TABLE tablename ENGINE = MyISAM 


4、查看mysql的错误日志
show variables like 'log_error'; 


5、查看mysql的运行状态
show [session|global] status

等同于
mysqladmin extended-status



6、查看mysql的所有进程
show full processlist;
show processlist;


7、查看建表语句
show create table 表名;


8、sum求和默认值取0
COALESCE(SUM(c.vote_value), 0)


[b]其他[/b]

# rails解析json
parsed_json = ActiveSupport::JSON.decode(your_json_string)
# ruby解析json
require 'json'
hash = JSON.parse string
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值