命令窗口查看log日志

命令窗口检log文件


tail -f log/health_record.log
命令:tail -f log/要查看的log文件


计划任务


  • 以上计划任务执行命令为 rake old_img_up:article_img_update_qiniu

引擎使用

使用命令拷贝migration文件至宿主,产生对应的 tables


rake 引擎名:install:migrations 例:rake bbs:install:migrations
rake railties:install:migrations 无需考虑引擎名拷贝migration文件至宿主


rails_g_model

rails 创建数据表


  • 上图创建了一张表,表名为:see_elders
  • see_content,see_image为俩字符串类型的字段
  • t.references :company_doctor, foreign_key: true该代码将生成company_doctor这张表的外键及索引字段company_doctor_id
  • comment: "看望老人图片记录"该代码为字段描述
  • t.timestamps该代码讲生成 created_at,updated两个字段

rails 数据库迁移(删除外键,删除索引,更改子弹类型等)

字段操作


add_column :表名, :字段名, :字段类型,comment:’字段功能说明’ 添加字段
add_index :表名, :索引字段名 添加索引
add_foreign_key :表名, :关联表名(例:posts) 添加外键
rename_column :表名, :旧字段名, :新字段名 修改字段名
change_column :表名, :要修改的字段名, :要改成的字段类型, comment:”字段功能说明” 修改字段类型
remove_foreign_key :表名, :关联表名 删除外键
remove_index :表名, :索引字段 删除索引
remove_column :表名, :字段名 删除字段


表操作


rename_table :旧表名, :新表名 修改表名
drop_table :表名 删除表


php求1-12之间所有和等于12的子集


  • <?php
  • $arr = array(1,2,3,4,5,6,7,8,9,10,11,12);
  • $n = count($arr);
  • $sub_n = pow(2,$n);
  • $sub_array = array();
  • for($i=0; $i<$sub_n; $i++){
  • $m = sprintf(‘%0+’.$n.’b’,$i);
  • $t_arr = array();
  • for($j=0;$j<$n;$j++)
  • if($m{$j}==1 && $j!=$n) $t_arr[] = $arr[$j];
  • if(array_sum($t_arr)==12) echo ‘{‘.implode(‘,’, $t_arr).’}’;
  • }
  • ?>

输出结果为:{12}{5,7}{4,8}{3,9}{3,4,5}{2,10}{2,4,6}{2,3,7}{1,11}{1,5,6}{1,4,7}{1,3,8}{1,2,9}{1,2,4,5}{1,2,3,6}

对象组装数组

pluck与map的区别

<%= item.item_contents.pluck(:content)%> 直接拿出content字段结果为数组
<%= item.item_contents.map{|m| m.content}%> 拿出整条数据遍历出content字段结果为数组
区别:pluck写法更为简介,效率稍微较map和collect高一点。
相同:所实现的结果相同

个人感觉map灵活性更强。

rails 开发笔录


ruby on rails 开发时记录下的内容

  • 脚手架的使用
  • 使用例子如下:
  • rails g scaffold user name:string 这条命令会生成user modle,controller,view以及数据库文件

  • rake db 给已有数据表添加字段
  • 运行:rails generate migration add_fieldname_to_tablename
  • 去生成的DB文件修改model名,以及要添加的字段备注等等
    def change
    `add_column :departments, :desc, :string,comment:'部门描述'`
    
    end
  • 执行数据库迁移命令:rake db:migrate

  • 使用脚手架只生成controller rails g scaffold_controller aa 即:只生成aa这个controller
  • 字段名_before_type_cast 对象后加此代码即可查看枚举前的值

谷歌搜索技巧


使用谷歌搜索时的一个小技巧

  • 在使用google搜索一些GEM时在搜索条件中加入site:+期待得到某网站结果的网址
  • 使用例子如下:
  • hexo site:github.com 这样搜索到的结果都是github的记录

rails render报错解决办法记录

使用ruby on rails 开发项目时double render错误解决办法

分割线

  • 如果需要强行使用render时 在render之后加and return 即可
  • 解决办法如下:
  • render json: @account.errors,status: :unprocessable_entity and return
|