1 minute Ruby on Rails lecture
You have 60 seconds to read it. Tic...Tac...Tic...Tac.... Time's Up. Got it!
Ruby on Rails
Originally uploaded by WanCW.
My sharing about how to transform myself into 'ror'aholic!
You have 60 seconds to read it. Tic...Tac...Tic...Tac.... Time's Up. Got it!
Ruby on Rails
Originally uploaded by WanCW.
下載ferret.gem , acts_as_ferret plugin
gem : gem install ferret
plugin : svn://projects.jkraemer.net/acts_as_ferret/trunk/plugin/acts_as_ferret,直接check out下來放到vendor/plugin 目錄下即可
現有一個model:
class Provider< fields =""> [:name,:description,:address,:website,:score,:phone,:direction]
.......... ............
end
這裡:fields就是要進行索引的對象屬性了,當然你也可以不指定,直接就一句"acts_as_ferret",默認不指定的時候是對所有列進行索引,先時事效果:
>>ruby script/console
>>Provider.find_by_contents(" 你要 搜索 的內容")
OK,如果沒有意外的話, 結果會返回一個ActsAsFerret::SearchResults對象。如果出錯了,回過去檢查一下,短短幾步,檢查起來也應該很方便。
幾個tip:
# "關鍵詞1" AND “關鍵詞2”
Provider.find_by_contents(" 關鍵詞1 關鍵詞2")
#"關鍵詞1" OR "關鍵詞2"
Provider.find_by_contents("關鍵詞1 OR 關鍵詞2")
#貌似“關鍵詞1”的結果
Provider.find_by_contents("關鍵詞1~")
上面這幾個都是小兒科啦
下面我們到acts_as_ferret plugin內部去看看,
acts_as_ferret/lib 瞄到 acts_as_ferret.rb 第116行(VERSION=181)
“ActiveRecord::Base.extend ActsAsFerret::ActMethods”
哈哈,許多blog中都建議在要進行分詞查詢的model中加入下面一段代碼:
def full_text_search(q, options = {})
return nil if q.nil? or q.strip.blank?
default_options = {:limit => 10, :page => 1}
options = default_options.merge options
options[:offset] = options[:limit] * (options.delete(:page).to_i-1)
results = self.find_by_contents(q, options)
[results.total_hits, results]
end
不過,本著DRY的原則,我決定從“ActiveRecord::Base.extend ActsAsFerret::ActMethods”這句話上做文章,既然ActiveRecord::Base 繼承了 ActsAsFerret::ActMethods ,何不將這段代碼加入到ActMethods裡去,讓所有model都擁有 full_text_search的功能?
module ActsAsFerret #:nodoc:
# This module defines the acts_as_ferret method and is included into
# ActiveRecord::Base
module ActMethods
def full_text_search(q, options = {})
return nil if q.nil? or q.strip.blank?
default_options = {:limit => 10, :page => 1}
options = default_options.merge options
options[:offset] = options[:limit] * (options.delete(:page).to_i-1)
results = self.find_by_contents(q, options)
[results.total_hits, results]
end
................ ....................
end
end
重啟一下,Provider.full_text_search方法應該出來了,有問題的話自己回去檢查看看
下面就是俗套,在application.rb中加入:
def pages_for(size, options = {})
default_options = {:per_page => 10}
options = default_options.merge options
pages = Paginator.new self, size, options[:per_page], (params[:page]||1)
pages
end
這些種種工作都是為了在controller以及view中調用:
controller:
def search @query=params[:query]
@total, @providers = Provider.full_text_search(@query, :page => (params[:page]||1))
@pages = pages_for(@total)
end
view:
<%= pagination_links(@pages, :params => {:query=>@query}) %>
關於 find_by_contents(q, options = {}, find_options = {})
中的find_options,考慮一下一種情況:
provider表結構:
id name description category_id
如果要對特定category_id的數據進行檢索,應如何?這裡就用到了find_option了:
代碼:
Vendor.find_by_contents(" 要進行檢索的關鍵字 ",{:limit=>30},{:joins=>"providers join categories on providers.category_id = categories.id ", :conditions =>" providers.category_id = XXXXX "})
其他一些小技巧大家可以到到網上找找,這裡就不一一列舉了。
發佈者:
WK
於
7:49 下午
標籤: acts_as_ferret
Here is a generic Amazon E-commerce REST API for ruby. It supports configurable default options and method call options. It uses Hpricot to parse the XML output. Use Response and Element wrapper classes for easy access to the XML elements, and it supports ECS 4.0. read more ...
Ruby on Rails Security Blog is a new blog for addressing security issues about ror. However, most of the topics are related to MySQL currently.
| # in Mar 2007 | # in Mar 2006 | Delta in Position | Language | Ratings Mar 2007 | Delta Mar 2006 | Status |
|---|---|---|---|---|---|---|
| 1 | 1 | Java | 18.044% | -3.84% | A | |
| 2 | 2 | C | 15.633% | -2.16% | A | |
| 3 | 3 | C++ | 11.109% | -0.05% | A | |
| 4 | 4 | PHP | 9.458% | -0.49% | A | |
| 5 | 5 | (Visual) Basic | 8.147% | -1.74% | A | |
| 6 | 6 | Perl | 6.420% | 0.00% | A | |
| 7 | 8 | | Python | 3.897% | +0.80% | A |
| 8 | 10 | | JavaScript | 3.485% | +1.75% | A |
| 9 | 7 | | C# | 3.365% | +0.22% | A |
| 10 | 21 | 11 * | Ruby | 2.773% | +2.31% | A |
| 11 | 11 | SAS | 1.849% | +0.51% | A | |
| 12 | 9 | | Delphi | 1.760% | -0.08% | A |
| 13 | 12 | | PL/SQL | 1.549% | +0.56% | A |
| 14 | 20 | 6 * | D | 1.268% | +0.78% | A |
| 15 | 19 | | ABAP | 0.777% | +0.26% | A |
| 16 | 15 | | Lisp/Scheme | 0.726% | +0.15% | A- |
| 17 | 14 | | Ada | 0.654% | +0.06% | B |
| 18 | 17 | | COBOL | 0.647% | +0.12% | B |
| 19 | 16 | | FoxPro/xBase | 0.600% | +0.06% | B |
| 20 | 31 | 11 * | Transact-SQL | 0.586% | +0.38% | B |
Both languages are boosted by their corresponding frameworks, Ruby On Rails and Ajax. This might be a new trend. In the recent past it was necessary to have a large company behind the language to get it in the spotlight (Sun with Java, Microsoft with C#), but nowadays a killer app appears to be sufficient. Viral marketing via the Internet works!
標籤: tiobe index
設定 Mongrel Cluster
請將你的 Mongrel Cluster 設定好,這裡預設 port 從 4000 ~ 4009 ,一共十個,跑在 production 環境下mongrel_rails cluster::configure -e production -p 4000 -N 10
修改 Apache 2.2 設定檔
mongrel_rails cluster::start
Apache 的設定檔放在 httpd.conf ,以下修改內容皆在 httpd.conf 裡面設定。
首先確定你的 Apach 2.2 有 enable apache 的其中一個 Module mod_proxyLoadModule proxy_module modules/mod_proxy.so
再來我們開始設定 Mongrel Cluster 的 reverse proxy 設定,首先我們給這組 cluster 取名叫做 examplecluster,他是10組 Mongrel ,跑在 port 4000 ~ 4009 之中
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
再來假設你的 hostname 為 example.com,我們開始設定 virtual host
# cluster member 1
BalancerMember http://127.0.0.1:4000
BalancerMember http://127.0.0.1:4001
....
BalancerMember http://127.0.0.1:4009
重 點是在於 ProxyPass,ProxyPass 代表 images,stylesheets,javascript 等 static file 交給 Apache 處理,不要給 Mongrel 處理。 還有 ProxyPassReverse 要指定正確的 cluster name ,我們要指定為 examplecluster。
ServerName example.com
ServerAdmin root@example.com
DocumentRoot "/var/www/example.com/htdocs"
ProxyPass /images !
ProxyPass /stylesheets !
ProxyPass /javascripts !
ProxyPass / balancer://examplecluster/
ProxyPassReverse / balancer://examplecluster/
最後重起 Apache 即可。本設定改自著名的文章 Scaling Rails with Apache 2.2, mod_proxy_balancer and Mongrel,還有 Robin 的 在Windows平台使用Apache2.2和Mongrel运行Ruby on Rails。設定是在 Gentoo Linux + Apache 2.2.3 + Mongrel 跑完全沒有任何問題。
[via: http://lightyror.thegiive.net/2006/12/apache-22-mongrel.html]
看完上一篇,大家一定很想丟雞蛋
這樣的 tutorial 也可以出來混一篇文章
在大家浪費買蛋錢之前,我必須要說,好戲在後頭呀!!!
Mongrel 如果只有單獨啟動在 80 port
他充其量不過是一個速度較普普的 Web Server
但是,當作 Backend Application Server 才是他的宿命呀
今天的需求是這樣
我們可以開啟數個 Mongrel Process 在其他 port
前端開一個 Proxy Server
當 Proxy Server 接受到 request
他會傳給 backend application server
其實作法很像 Mod_fastcgi 的作法
只是中間的 application server 將 fastcgi 改成了 mongrel
如果要開啟眾多的 Mongrel Process
我們當然可以用
mongrel_rails start -d -p 8000 -P log/mongrel_1.pid這樣來執行,但是要管理就變得相當的麻煩
mongrel_rails start -d -p 8001 -P log/mongrel_2.pid
....
gem i mongrel_cluster即可
mongrel_rails cluster::configure -e 那些環境 \他會將 config 寫到 config/database.yml
-p 8000 \
-N 3 \
-c /var/www/servers/ \
-a 127.0.0.1 \
--user mongrel \
--group mongrel
mongrel_rails start -d -p 8000 -P log/mongrel_1.pid一樣的意義
mongrel_rails start -d -p 8001 -P log/mongrel_2.pid
mongrel_rails start -d -p 8002 -P log/mongrel_3.pid
Mongrel is a fast HTTP library and server for Ruby that is intended for hosting Ruby web applications of any kind using plain HTTP rather than FastCGI or SCGI. It is framework agnostic and already supports Ruby On Rails, Og+Nitro, and Camping frameworks.他跟Webricks 一樣,是一個方便我們架設 Ruby on Rails 環境的網頁伺服器
gem i mongrel使用方式呢,基本上跟 Webrick 差不多
mongrel_rails start你就會發現他已經可以使用了
mongrel_rails start -d即可
mongrel_rails start -p 1234即可
mongrel_rails start -e production/development/test即可
mongrel_rails start -l log/mongrel_log即可
mongrel_rails start -h即可
mongrel_rails restart
mongrel_rails stop
My steps to install Ruby On Rails 1.8.5 over ubuntu 6.0.6
Prep
Make sure you have gcc complier and zlib development package installed. If not sure, please run
apt-get install build-essestialRuby
apt-get install zlib1g-dev
tar xzvf ruby-1.8.5-p12.tar.gzIf you saw lots of text fly by but didn’t get that error, it means that we should now have a brand new Ruby installed.
cd ruby-1.8.5-p12
./configure --prefix=/usr/local --enable-pthread
make
sudo make install
sudo make install-doc
cd ..
We can verify this (as well as a correct path setting) by typing the following command:
ruby -vYou should see something like this:
ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-linux]zLib
tar zxvf ruby-zlib-0.6.0.tar.gzRubyGems
cd ruby-zlib-0.6.0
ruby extconf.rb
make
sudo make install
cd ..
RubyOnRailstar xzvf rubygems-0.9.2.tgz
cd rubygems-0.9.2
sudo /usr/local/bin/ruby setup.rb
cd ..
* gem respects the following variables for proxy settingssudo gem install rails --include-dependencies
標籤: installation
一般來說,Ruby on Rails 架設原理很簡單,分成三個部份。


Lighttpd 1.4 跑 PHP 或是跑 Ruby on Rails 都是這樣跑。Lighttpd 為 Frontend Server ,他接到 request ,經由 unix socket 送給身為 Application Server 的 Fastcgi ,Fastcgi 再執行 Ruby on Rails 程式。通常這個方式是 Frontend Server 跟 Application Server 跑在同一個機器上面的,當然每個 Fastcgi 也可以跑在 TCP Mode ,然後 Fastcgi 放在不同機器上面,不過設定不易不常使用這個方式。這個組合的好處是執行效率非常快速,如果 Frontend 跟 Application 放在同一台機器上,Lighty 這個組合遠比 Apache 的組合來得輕快許多。但是缺點是多台機器設定複雜,不易擴展到多台機器上,Fastcgi 又有在高負載下罷工的負面報導。相反的,單台機器上面 Fastcgi 設定管理簡單,速度又飛快,非常適合小網站剛剛起步時,機器不足,流量不大的需求。設定方式在此我有介紹。
類 似Apache 的組合,但是 nginx 或是 Lighttpd 1.5 都有一個優點,不像 Apache 2.2 那麼肥。反正這個 Frontend 只要作 reverse proxy 的工作,其實不需要 Apache 那麼大的 Server 來作。好處是 Frontend Server 比較快速。壞處是 nginx 這個俄國來的 Server 大家不熟悉,doc 又很少(俄文是很多啦)。Lighttpd 1.5 又只在 pre-release 階段,穩定程度還是得花點時間。不過如果扣掉 Frontend 的 X factor,這個組合兼具輕快跟方便 Scale ,可說是最好的組合。lighty 1.5設定方式在此我有介紹。nginx 的設定檔這裡有範例,可以參考。標籤: architecture, installation
class AddTagSupport < force =""> true do t
t.column :name, :string
end
create_table :taggings, :force => true do t
t.column :tag_id, :integer
t.column :taggable_id, :integer
t.column :taggable_type, :string
t.column :created_at, :datetime
end
create_table :products_tags, :id => false, :force => true do t
t.column :tag_id, :integer, :default => "0", :null => false
t.column :product_id, :integer, :default => 0, :null => false
end
# Index your tags/taggings
add_index :tags, :name
add_index :taggings, [:tag_id, :taggable_id, :taggable_type]
end
def self.down
drop_table "tags"
drop_table "taggings"
drop_table "products_tags
end
end
You really only need the first two tables to have it up and working.
James Edward Gray shows how to apply unit tests to solving Ruby Quiz #84: Generating Pascal’s Triangle. In the process he also demonstrates a lot of features from the Ruby bundle and explains conventions and rationale behind them.
The Ruby Quiz screencast (42 MB) is almost an hour long, so you may want to grab a snack before you start it :)
標籤: screencast, testing, tutorial
This is a a very good site from san diego ruby users group - http://podcast.sdruby.com/
Episodes includes
Enjoy!