2008年4月27日 星期日

chop, average

http://www.ruby-doc.org/core/classes/Kernel.src/M006008.html
chop 可以幫妳去掉換行字元, 或者把字串最後的一個字元刪掉

http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html#M001338
average 可以幫妳計算某個欄位的平均

2008年4月24日 星期四

action, Marshal, overwrite_params

想自己用string湊出action, 用send(:symbol)
act = "act"+"ion"
send(act.to_sym)

----------------------------------
Marshal
有時候我們會需要把ruby object 存到到其他地方去像memcache, 而在memcahce 會被存為string, 如果想要拿出來時可以是ruby object 就需要用到Marshal

---------------------------------
overwrite_params
可以讓我們只取代url 部份的參數, 在做一些sort query 只想改變某些條件時,很方便

2008年4月22日 星期二

jquery, javascript

link_to(e) 可以藉由這個e 取得呼叫 link_to 的 element
$(e.target).attr('id'); 取得該element attribute id的值

window.location 連到後面的url

$('#today').attr({class:"TabbedPanelsTab"});
把id為today 的element 裡面的attribute class 值變為 TabbedPanelsTab

$('#TabbedPanelsTab').bind('click', link_to);
把id為TabbedPanelsTab的element event 綁上 link_to


[script type="text/javascript"]
(function($){
function link_to(e) {
var id = $(e.target).attr('id');
window.location = "http://yhd.icetech.com.tw:20503/user/"+id+"/<%= params[:sort]%>/<%= params[:type]%>";
})
}
$(document).ready(function() {
$('#today').attr({class:"TabbedPanelsTab"});
$("#<%= params[:during] %>").attr({class:"TabbedPanelsTab TabbedPanelsTabSelected"});
$('.TabbedPanelsTab').bind('click', link_to);

})

})(jQuery);
[/script]

yield, block and Benchmark

yield 和 call back 都可以像是把function, 一段code 當參數一樣丟進去
而benchmark則可以幫我們測量一下時間

require 'benchmark'

def yyield
yield "a"
yield "b"
end


Benchmark.bm do |x|
x.report {
yyield do |k|
puts k
end
}
end

def block(&block)
block.call("a")
block.call("b")
end

Benchmark.bm do |x|
x.report {
block do |k|
puts k
end
}
end


第一次結果是
user system total real
a
b
0.000000 0.000000 0.000000 ( 0.000858)
user system total real
a
b
0.000000 0.000000 0.000000 ( 0.000231)

之後
user system total real
a
b
0.000000 0.000000 0.000000 ( 0.000030)
user system total real
a
b
0.000000 0.000000 0.000000 ( 0.000024)

2008年4月20日 星期日

type, key, connection

type, key, connection 是關鍵字column name 別取ㄟ

2008年4月17日 星期四

map, ||=

@users = User.find(:all)
@uses.map(&:id) 跟 @users.map{ |u| u.id} 意思相同

---------------------
||=
a ||= b
如果a沒有值(nil) a的值就是b,
如果a有值就沒事

2008年4月16日 星期三

find

http://www.ruby-doc.org/core/classes/Enumerable.html#M003153
不只可以 Class.find 一下 只要是集合 也可以find一下

@users = User.find(:all)

number_100_man = @users.find{ | u | u.id == 100}

Erubis

http://www.kuwata-lab.com/erubis/

開始要重視效能的時候, 就可以用這個了

content_for

http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#M001069

content_for 可以把 要用的內容先存起來 要用的時候就可以叫出來,
我知道的用法, 像是用在 妳有許多view 會include到不同的javascript
不想在layout 一口氣include全部, 想要根據各個不同的view 去include 就可以用這l個
例如

view_1
<% content_for :load_script do %>
<%= javascript_include_tag "view_1" %>
<% end %>

view_2
<% content_for :load_script do %>
<%= javascript_include_tag "view_2" %>
<% end %>

layout
<%= yield :load_script%>

由於會先去load view 的template 再去load layout 這樣就可以 根據不同的 view 去include 所需要的

2008年4月15日 星期二

裝envy

參考
http://sjcywg.blogspot.com/2008/04/envy.html
http://albertomilone.com/pmwiki/pmwiki.php?n=Main.EnvyNG-InstructionsForUbuntu

Partial , find by list

用locals 參數可以在加些想傳入的變數
layout 可以指定要哪個layout

想要按照某個list 順序拿object時, 會發現find回來的並不是按照丟進去的順序的
這時候就要自己來

user_list = "444,761,137,642,73"

users_rank_list = user_list.split(',')
users = self.find(*user_rank_id_list)
users_list = []
user_rank_list.each do |rank|
users_list << users.find{|u| u.id == rank}
end
users_list

2008年4月14日 星期一

一些好用的helper

constantize :
Constantize tries to find a declared constant with the name specified in the string
把字串可以變成classㄟ
"Class".constantize #=> Class

還有
classify
"egg_and_hams".classify #=> "EggAndHam"

pluralize
"post".pluralize #=> "posts"

singularize
posts".singularize #=> "post"

foreign_key
"Message".foreign_key #=> "message_id"


一些method 可以幫助妳去組 controller, model class的name還是些有的沒的

定時寄信 crontab scriptrunner

http://linux.vbird.org/linux_basic/0430cron.php
http://wiki.rubyonrails.org/rails/pages/runnerscript
crontab -e
0 12 * * 7 cd /home/your_name/your_rails_app/ && script/runner 'Record.weekly_report' -e development

Model, record.rb
def self.weekly_report
 users = User.find(:all)
 users.each do |u|
  EbMail.deliver_weekly_report(u)
 end
end

2008年4月12日 星期六

Partial 怎麼用, example

[controller]
def show
@users = User.find(:all)
end

[view, show.rhtml]
用:object是 @users 單純傳入 _objects.rhtml 裡, 而在partial就叫做 objects
<%= render :partial => "objects", :object => @users %>


[view, _objects.rhtml] collection = @user.each 被拆成一個個後傳入
用:collection 是 object 被一個個拆開後才丟入 _collection.rhtml, 而在裡面被拆開後的local variable就叫做 collection
<%= render :partial => "collection", :collection => objects%>

[view, _collection.rhtml], _collection 就像是已經有個迴圈包起來一樣
<%= collection.name %>
<%= collection.address %>


<%= render :partial =>"collection", :collection => objects %>

<% objects.each do |e| %>
<%= e.name%>
<%= e.address %>
<% end %>
結果一樣只是連迴圈都省了

2008年4月9日 星期三

rails-toolkit.vim

rails-toolkit.vim 裏面 有個設定: number 會自動標上行號 很煩, 拿掉就好