夜鸣猪的Ruby On Rails 空间
夜鸣猪 · 共 628 篇文章
查找多文件内容替换
UPDATE: (05/03/2013) 当前使用: #去掉所有的FactoryGirl. find . -type f -name *_spec.rb -print | xargs sed -ie 's/FactoryGirl\.//g' //Seems like Linux is being far too generous. -i 's/^[[:space:]]\{11\}//g' <-- makes it look like "s/^[[:space:]]\{11\}//g" is the extension given to "-i" //baf's quotes serve to…
2012-09-14 20:58 · Linux · 评论(0)
shell script
select r.name, count(p.id) from persons as p join rides as r on p.fav_ride_id = r.id group by r.id order by count(p.id) desc limit 2; 一整就用到 $PWD $PATH $HOME The shell has variables to access arguments in the procedures, to define the environment and the like. Shell variables are: $1 # argument ($0.…
2012-09-11 21:08 · Linux · 评论(0)
定期清空日志文件
一个关于如何在指定文件大于1GB后,自动删除的问题。 批处理代码如下: #!/bin/bash # 当/var/log/syslog大于1GB时 # 自动将其备份,并清空 # 注意这里awk的使用 if ! [ -f /var/log/syslog ] then echo "file not exist!" exit 1 fi if [ `ls -l /var/log/syslog|awk '{print $5}'` -gt $((1024*1024)) ] then cat /var/log/syslog >> ~/log/history # 将日志备份 echo >> ~/log/his…
2012-09-11 10:04 · Linux · 评论(0)
查看一个进程占用了那个端口
有时候需要在Linux下查看一个进程占用了那个端口,但是只知道进程大致的名称,比如要查看hadoop的namenode在哪个端口上运行,以便在eclipse中连接。 首先用ps命令查看进程的id: $ ps -ef | grep Name 其中每一行(很长的时候会占用若干行)的第二个字段就是进程的id。 当然,对于在java虚拟机中运行的进程,比如hadoop的守护进程,可以直接用jsp命令查看: $ jps | grep Name 或者已经知道进程的确切名称,可以用pidof查看: $ pidof Name 查看到进程id之后,使用netstat命令查看其占用的端口: $ netstat …
2012-09-11 09:59 · Linux · 评论(0)
说rails文件上传
当前ruby1.9.3 rails 3.2.8,流行的文件上传插件是 paperclip 和 carrierwave 然而,简单的这是需要上传个文件的话,可以用更容易的办法 <%= form_tag({:action => :upload}, :multipart => true) do %> <%= file_field_tag 'picture' %> <% end %> <%= form_for @person do |f| %> <%= f.file_field :picture %> <% end %> def upload uploaded_io = params[:person…
2012-09-10 06:46 · 评论(0)
postgresql怎么用change.column
def up connection.execute(%q{ alter table contests alter column prize type integer using cast(prize as integer), alter column price set default 200 }) end def up case ActiveRecord::Base.connection when ActiveRecord::ConnectionAdapters::PostgreSQLAdapter connection.execute(%q{ alter table contests a…
2012-09-10 06:33 · 评论(0)
Ruby里的structure和set
# Create a structure with a name in Struct Struct.new("Customer", :name, :address) #=> Struct::Customer Struct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main"> # Create a structure named by its constant Customer = Struct.new(:name, :address) #=> Custo…
2012-09-10 06:29 · Ruby On Rails · 评论(0)
新旧两种方式定义singleton method
class A class << self def class_name to_s end end end A.define_singleton_method(:who_am_i) do "I am: #{class_name}" end A.who_am_i # ==> "I am: A" guy = "Bob" guy.define_singleton_method(:hello) { "#{self}: Hello there!" } guy.hello #=> "Bob: Hello there!" class Fred def initialize(p1, p2) @a, @b =…
2012-09-10 06:23 · Ruby On Rails · 评论(0)
rails里使用truncate table
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table_name}")
2012-09-07 02:04 · Ruby On Rails · 评论(0)
Rails polymorphic的使用和理解
class Person < ActiveRecord::Base has_one :address, :as => :addressable end class Company < ActiveRecord::Base has_one :address, :as => :addressable end class Address < ActiveRecord::Base belongs_to :addressable, :polymorphic => true end Address 有adressable_id addressable_type其中,addressable_type可以为…
2012-09-07 02:03 · Ruby On Rails · 评论(0)
git rebase 和 merge的区别
http://stackoverflow.com/questions/457927/git-workflow-and-rebase-vs-merge-questions git fetch用来将自己本地的repo 更新到最新的。 但是fetch后,并不会显示到本地。 有两种办法, rebase 和 merge. if you want masterA => masterB => masterC => yourworkhere => (masterD and E merged) => HEAD then use merge if you want masterA => B => C => D …
2012-09-04 21:34 · Ruby On Rails · 评论(0)
打开postgresql的远程访问权限
External PostgreSQL connections To be able to reach the server remotely you have to add the following line into the file: /var/lib/pgsql/data/postgresql.conf: listen_addresses = '*' PostgreSQL, by default, refuses all connections it receives from any remote address. You have to relax these rules by…
2012-09-04 21:30 · Ruby On Rails · 评论(0)
在ubuntu下跑 Postgresql 和 Rails3 基本
Rails 3 Installation sudo gem install rails postgres as db installation<>br/ $ sudo apt-get install postgresql Rails 3 App with postgres as database $ rails new pg -d postgres bundle installation It will install dependency gems & postgres adapter for db connection bundle install Here by default 'po…
2012-09-04 21:06 · Ruby On Rails · 评论(0)
Ruby yaml转换xml
require 'active_support/core_ext' YAML.parse("key: value").transform.to_xml
2012-09-04 21:02 · Ruby On Rails · 评论(0)
Could not find a JavaScript runtime
错误提示: I, [2012-09-04t08:52:02.594025 #3839] INFO -- : listening on addr=0.0.0.0:3000 fd=5 I, [2012-09-04t08:52:02.594204 #3839] INFO -- : worker=0 spawning... I, [2012-09-04t08:52:02.594679 #3839] INFO -- : master process ready I, [2012-09-04t08:52:02.595505 #3841] INFO -- : worker=0 spawned pid=38…
2012-09-04 21:01 · Ruby On Rails · 评论(0)