重慶淶灘古鎮和二佛寺

一個多月前去的淶灘古鎮, 到今天才傳上了照片.

重慶淶灘古鎮和二佛寺
淶灘古鎮的城門 繼續閱讀 →

發表在 人在旅途, 浮光掠影 | 留下評論

如何復興越劇?

前天晚上和網友聊天時得知今天下午一個劇院里有越友演出,今天開了鬧鐘.起床時11點. 起床後連連打哈欠.

做飯, 吃飽後轉着廣場幾個公車站找能到那個劇院的公車. 終於找到了, 可惜下錯了站, 之後又上錯上了另一輛公車, 最後才換了一個能到的. 到時已經2點多, 幸運的是原定1:30的演出剛好拖到此時要開幕.

場不算大, 但坐得算很滿. 有一好心的大伯看我找座, 忙把我讓到他身邊的一個空位.

劇場上拉是一橫幅, 從這字幅看這是為紀念越劇百年, 貴陽, 重慶, 成都三地的越友共同自費組織的一場表演.
如何復興越劇?
開幕了, 上來兩位很標準的主持念"經典"的台詞

表演進行了大概三小時. 都是越劇選段, 選的最多的是梁祝. 表演者大多掛着XX劇團的名字, 最多的是一重慶的劇團. 表演者的水平就我個人聽來都不怎麼樣. 給我印象最深的倒是其中唯一一對沒有掛名的外地"越友"的演唱. 唱者聲音洪亮, 字正腔圓, 韻味十足.他們演唱結束後掌聲雷動, 贊聲連連. 但最受觀眾歡迎的好像不是這一對, 而是一位來自本地的早期專業越劇演員. 聽說以前她在重慶是紅到發紫. 她上去清唱了一段. 除了不斷的拉長音虛詞, 我沒聽明白一個字. 但她下台後台下幾乎沸騰了. 不少觀眾叫着"再來一個, 再來一個." 結果我擔心的事件沒有發生 –她說自己沒有準備, 沒有再來一個. 她讓我明白了一個道理: 做網站不要看網站站長的背景功底, 而要看他做出來的網站; 或很多人技術不錯, 但就不用來做正事, 都用到歪道上, 做出來的網站不一定有用. 正如越劇不是把詞拉得越長越好聽, 但功力好的演員卻常常要為了表現自己功力而掃觀眾的興.
如何復興越劇?
這些演員將反覆上台演出

如何復興越劇?
這兩位應該就是我說的唱得很不錯的越友

如何復興越劇?
再來一張, 可惜還是沒照清楚

如何復興越劇?
這個是從 碧玉簪 里選的." 婆婆"表情還是比較非富的.

如何復興越劇?
可憐的祥林嫂

正如剛才說的,觀眾來了不少. 但基本上全是中老年, 其中又以女性居多. 或我, 一個二十多歲的男青年坐在劇場很顯眼, 兩個拿DV記錄的都跟到我這邊留個錄像.
如何復興越劇?
基本上全是中老年觀眾

這事讓我很擔心.
為什麼看越劇的都是中老年呢? 這對越劇有什麼影響呢?
究其原因有兩種可能:
第一種可能是越劇本來就是為老年人開發的藝術. 從100年前, 到100年後的現在, 越劇就一直只是中老年人喜愛的. 人非要過40歲才有可能戀上越劇.
如果真是這樣,應該也算是一件好事啊. 中國的老齡化問題聽說很嚴重啊, 以後的老人會越來越多. 這樣的話越劇的潛在觀眾也會更多.
第二可能是越劇本來是全民文化, 但後來受到電影,電視, 現代流行音樂, 電子遊戲等其它娛樂項目的競爭,在現代社會中對新生代幾乎沒有了任何吸引力. 新青年都對越劇不感興趣, 沒有或很少新越劇迷加入, 從而越劇只被為固執的老資格戲迷所喜愛, 而這些人逝老, 形成只有中老越劇迷這種青黃不接的情景.
如果是這個情形的話越劇是把房子蓋在沙堆上, 很危險啊.

無論上面是那個情況, 如果越劇能讓青年喜歡, 都是好事. 為什麼越劇不能吸引青年呢?
我上面提到過電影, 電視, 音樂, 遊戲等的競爭, 如果這些東西都當作敵人, 而且他們的攻擊力很強, 越劇現居弱勢, 長此下去, 必將不敵啊.
所在我感覺越劇要興, 必仗一字 — 變!
越劇很美, 但美中有不足, 有不讓人喜歡的原因.
慢
就比如拉長音, 雖然運用得當, 很是可以強烈地加強人物情感. 但也是越劇在當今的一大把柄. 隨着商品經濟的發展, 人們進了快節奏的生活方式, 但戲劇太"慢"了. 一部戲近3個小時, 可實際上沒說上幾句話.假如唱一句要半分鐘,年青觀眾沒這個耐性子. 年青人說:"我一分鐘幾百萬上下, 還聽你這樣磨嘴皮?"
(娘子——————————啊…(開唱), 沒等開唱, 那"子—"就能嚇跑一半的青年人, 你說這個"子—"除了嚇人, 還有什麼功能作用的???)
(五女拜壽 做得很好.)
俗
劇本少,劇本改來改去, 沒什麼新鮮情節. 劇情俗, 本來故事就不多, 而且還都很不合時代, 故事大多是給非常傳統的人寫的.
越劇沒有電影那樣的驚心場面, 沒有遊戲裡面的激情互動. 但這兩個不算缺點. 本來越劇就是通過樸素的背景服裝唱詞唱法來敘述看來平淡, 聽完卻感天動地之情的藝術. 有特色才有存在的價值.

我一直希望越劇能重興, 但我說的重興是怎樣一個度呢?
從舞台表演到屏幕表演的改革(媒體傳播向網絡化發展, 舞台表演本身就限制越劇的傳播)
屏幕後不再重唱梁祝(因為有錄相了), 每年像賀歲片一樣, 一年一部新片. 新故事, 新創意, (新演員).

人百年或已德高望重,  越劇百年或已將長存之道蘊含在自身的歷史之中.

看完演出, 走出大門, 我徑直又上了一輛公車. 到了終點站才發現又坐錯了. 這才等來一輛能到沙坪壩的公車. 這車也不對–它竟開了一個多小時才到沙坪壩. 我把渴的眼睛發綠.

發表在 越劇柔情 | 留下評論

成功的條件

今天在想成功的條件, 現在總結有這幾條:
成功在這裡指完成一件事.
1.堅定決心
一要很想完成這事,慾望很強; 二要對這事堅貞不逾, 事不成不罷休, 包括在遇到多大的困難和挫折時都不改志向.
2.人際關係
好人際關係可以得到很多的幫助, 使事件的發展勢勢如破竹.
3.人力物力
做事的基礎. 包括自己的技能(人力之一).
4.計謀策略
做事的方法
5.天時地利

發表在 成敗幾何 | 留下評論

舍 得

有時候舍就是得,得就是舍. 要取捨清楚.

發表在 成敗幾何 | 留下評論

如何長生不老

如何才能長生不老是我一直在研究的課題之一.
雖然我還未能找到長生的方法.但我找到了一個很重要的線索.
要做到長生不老,起碼要做到的一個就是:
不狂喜,不痛悲

情感是使人衰老,特別是悲愁. 我親眼看到過N個中年人因為一件愁心的事半年之後成為老年人. 由中年過度到老年更決定的因素不是年月,而是情感.

古書更有一夜白髮的事件無數, 我相信這是真實的.

要做到長壽,很重要的就是做到不狂喜,不痛悲.對得失都看得很淡泊.

要如何能做到這點呢?

本身的思想眼光應該是廣博的. 心懷宇宙. 對於百,一為少,對於萬,百為小,對於億,萬亦不足道. 對於年,日為短,於宇宙的長存,人之一生演的不過是百年的喜悲; 人活於浩瀚之天地,人的手腳舞動不過在尺寸之內. 之於無際的宇宙,人只是不起眼的一個小點, 算得了什麼? 這樣的一生得到什麼,又失去什麼, 一定要讓人狂喜,一定要讓人痛悲???

 每在不順心時我都會仰望天空, 看着廣闊的天空或浩瀚的星空,我的不順心有如宇宙間的一顆塵埃,微–不足道!

發表在 我思我在 | 2 條評論

Ruby on Rails頁面緩存

三種方式 Page Caching, Action Caching和 Fragment Caching

緩存默認只在production 環境下啟動

Page Caching
caches_page :public_content
以URL為準
expire_page :action =>"public_content"

Action Caching
caches_action :premium_content
以URL為準
 expire_action :action => "premium_content", :id => article
 Page Caching 和 Action Caching的實例:
class ContentController < ApplicationController
 before_filter :verify_premium_user, :except => :public_content
 caches_page :public_content
 caches_action :premium_content
 cache_sweeper :article_sweeper,
        :only => [ :create_article,
        :update_article,
        :delete_article ]
 def public_content
  @articles = Article.list_public
 end
 def premium_content
  @articles = Article.list_premium
 end
 private
 def verify_premium_user
  return
   user = session[:user_id]
   user = User.find(user)
  if user
   unless user && user.active?
   redirect_to :controller =>  "login", :action => "signup_new"
  end
 end
end

刪除過期緩存內容:
app/models中加article_sweeper.rb
class ArticleSweeper < ActionController::Caching::Sweeper
 observe Article
 def after_create(article)
  expire_public_page
 end
 def after_update(article)
  expire_article_page(article.id)
 end
 def after_destroy(article)
  expire_public_page
  expire_article_page(article.id)
 end
 private
 def expire_public_page
  expire_page(:controller => "content", :action => ‘public_content’)
 end
 def expire_article_page(article_id)
  expire_action(:controller =>  "content",
        :action => "premium_content",
        :id => article_id)
 end
end
app/public/content/show/1默認caches_page文件路徑
app/public/content/show/1.html

Fragment Caching
controller:
class Blog1Controller < ApplicationController
 def list
  @dynamic_content = Time.now.to_s
  unless read_fragment(:action => ‘list’)
   logger.info("Creating fragment")
   @articles = Article.find_recent
  end
 end
end
#read_fragment()查看一個action的fragment緩存是否存在
view:
<%= @dynamic_content %> <!– 動態內容 –>
<% cache do %> <!– 緩存開始–>
 <ul>
 <% for article in @articles -%>
  <li><p><%= h(article.body) %></p></li>
 <% end -%>
 </ul>
<% end %> <!– 結束緩存 –>
<%= @dynamic_content %> <!– 其它動態內容 –>

使用多個緩存段:
<% cache(:action => ‘list’, :part => ‘articles’) do %>
 <ul>
 <% for article in @articles -%>
  <li><p><%= h(article.body) %></p></li>
 <% end -%>
 </ul>
<% end %>
<% cache(:action => ‘list’, :part => ‘counts’) do %>
 <p>There are a total of <%= @article_count %> articles.</p>
<% end %>
#使用:part參數區分同一action下的不同緩存段

緩存過期
controller:
class Blog2Controller < ApplicationController
 def list
  @dynamic_content = Time.now.to_s
  @articles = Article.find_recent
  @article_count = @articles.size
 end
 def edit
  # 編輯文章時不更新統計緩存
  expire_fragment(:action => ‘list’, :part => ‘articles’)
  redirect_to(:action => ‘list’)
 end
 def delete
  #刪除文章時同時刪除兩個緩存
  expire_fragment(:action => ‘list’, :part => ‘articles’)
  expire_fragment(:action => ‘list’, :part => ‘counts’)
  redirect_to(:action => ‘list’)
 end
end

expire_fragment()可接正則表達式
expire_fragment(%r{/blog2/list. })

Fragment Caching存儲選項設置:
ActionController::Base.fragment_cache_store = <下面的選項之一>
ActionController::Caching::Fragments::FileStore.new(path)
ActionController::Caching::Fragments::DRbStore.new(url)
ActionController::Caching::Fragments::MemCachedStore.new(host)

發表在 Ruby on Rails | 留下評論

Ruby on Rails Action Controller學習筆記

自動增加預載model方法:
class StoreController < ApplicationController
 model :cart, :line_item
 observer :stock_control_observer
 # …

如果沒有找到相應的action, method_missing()將被調用
如果沒有任何action, Rails會直接找template顯示.

Ruby on Rails鏈接
Routing Requests
配置文件:config/routes.rb
ActionController::Routing::Routes.draw do |map|
 map.connect ‘:controller/service.wsdl’, :action => ‘wsdl’
 map.connect ‘:controller/:action/:id’
end

map.connect可用參數:
:defaults => { :name => "value", …}
 設默認值,默認為:defaults => { :action => "index", :id => nil }
:requirements => { :name =>/regexp/, …}
:name => value
:name => /regexp/

實例:
ActionController::Routing::Routes.draw do |map|
 #從上到下優先級降低.
 #’http://my.app/blog/&#39; 直接顯示 index
 map.connect "blog/",
   :controller => "blog",
   :action =>"index"
 #按日期訪問博客文章列表
 map.connect "blog/:year/:month/:day",
      :controller =>"blog",
      :action =>"show_date",
      :requirements => { :year => /(19|20)\d\d/,
               :month => /[01]?\d/,
               :day => /[0-3]?\d/},
      :day =>nil,
      :month =>nil
 # 按文章ID顯示文章內容
 map.connect "blog/show/:id",
      :controller => "blog",
      :action =>"show",
      :id => /\d+/
 # 管理頁面,常規
 map.connect "blog/:controller/:action/:id"
 # 其它
 map.connect "*anything",
      :controller =>"blog",
      :action =>"unknown_request"
 #=> URL>junk
 #=>@params = {:anything=>["junk"], :controller=>"blog", :action=>"unknown_request"}
end

鏈接生成url_for
@link = url_for :controller => "store", :action => "display", :id => 123
生成:http://pragprog.com/store/display/123
url_for(:controller => "store", :action => "list",
 :id => 123, :extra => "wibble")
生成:http://rubygarden.org/store/list/123?extra=wibble
url_for(:overwrite_params => {:year => "2002"})
生成:http://pragprog.com/blog/2002/4/15
#url_for會使用默認環境中的參數自動補充出完整的地址
#但一般補最後面的
#使用:overwrite_params使之補前面的.
url_for(:year=>year, :month=>sprintf("%02d", month), :day=>sprintf("%02d", day))
用來填充位數???
url_for(:controller => "/store", :action => "purchase", :id => 123)
#=> http://my.app/store/purchase/123
url_for(:controller => "/archive/book", :action => "record", :id => 123)
#=> http://my.app/archive/book/record/123

redirect_to(:action =>’delete’, :id => user.id)
# 和上面的一樣:
redirect_to(:action => ‘delete’, :id => user)

default_url_options()
:anchor string #+string
:host string  (helper.pragprog.com:8080)
:only_path boolean
:protocol string ("https://";)
:trailing_slash boolean (+"/"?)

有名字的 Routes
map.date "blog/:year/:month/:day",
    :controller =>"blog",
    :action =>"show_date",
    :requirements => { :year => /(19|20)\d\d/,
            :month => /[01]?\d/,
            :day => /[0-3]?\d/},
    :day =>nil,
    :month =>nil
可調用
date_url(:year => 2003, :month => 2)
#=> http://pragprog.com/blog/2003/2

方法
把action藏起來,讓它使用URL訪問不了.
hide_action :check_credit
def check_credit(order)
# …
end

Controller Environment
request
  domain()
  remote_ip()
  env() 如:request.env['HTTP_ACCEPT_LANGUAGE']
  method (:delete, :get, :head,:post, or :put.)
   delete?, get?, head?, post?, and put?
class BlogController < ApplicationController
 def add_user
  if request.get?
   @user = User.new
  else
   @user = User.new(params[:user])
   @user.created_from_ip = request.env["REMOTE_HOST"]
   if @user.save
    redirect_to_index("User #{@user.name} created")
   end
  end
 end
end

params
cookies
response
session
headers

runder()
更改默認template目錄
ActionController::Base.template_root=dir_path
render(:text=>string)
class HappyController < ApplicationController
 def index
  render(:text =>"Hello there!")
 end
end

render(:inline=>string, [ :type =>"rhtml"|"rxml"] )
class SomeController < ApplicationController
 if RAILS_ENV == "development"
  def method_missing(name, *args)
   render(:inline => %{  
    <h2>Unknown action:#{name}</h2>
    Here are the request parameters:<br/>
    <%= debug(params) %> })
  end
 end
end

render(:action =>action_name)
def display_cart
 if @cart.empty?
  render(:action => :index)
 else
  # …
 end
end

render(:file =>path, [ :use_full_path =>true|false] )

render(:template =>name)
class BlogController < ApplicationController
 def index
  render(:template =>"blog/short_list")
 end
end

render(:partial =>name, …)
render(:nothing => true)
render_to_string() 不發送,直接轉為string

發送
send_data
def sales_graph
png_data = Sales.plot_for(Date.today.month)
 send_data(png_data, :type => "image/png", :disposition => "inline")
end

send_file
def send_secret_file
 send_file("/files/secret_list")
 headers["Content-Description"] = "Top secret"
end

有些地方使用redirect_to代替render
redirect_to(:action => ‘display’)

redirect_to(options…)
redirect_to(path)
redirect_to(url)

Cookies and Sessions
Cookies只能存String
class CookiesController < ApplicationController
 def action_one
  cookies[:the_time] = Time.now.to_s
  redirect_to :action =>"action_two"
 end
 def action_two
  cookie_value = cookies[:the_time]
  render(:text => "The cookie says it is #{cookie_value}")
 end
end

cookies[:marsupial] = { :value => "wombat",
          :expires => 30.days.from_now,
          :path =>"/store" }
可用選項 :domain, :expires,:path, :secure, and :value

Sessions
保存model時要先預載:
class BlogController < ApplicationController
 model :user_preferences

設置session:
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:session_key] = ‘my_app’
可用選項:
:database_manager
:session_domain
:session_id
:session_key
:session_path
:session_secure
Session保存選擇:
:database_manager => CGI::Session::PStore
  flat file (單服務器推薦)
:database_manager => CGI::Session::ActiveRecordStore
:database_manager => CGI::Session::DRbStore
:database_manager => CGI::Session::MemCacheStore

Flash用於在兩個action傳輸暫時信息
flash.now用法實例:
class BlogController
 def display
  unless flash[:note]
   flash.now[:note] = "Welcome to my blog"
  end
  @article = Article.find(params[:id])
 end
end

flash.keep用法實例:
class SillyController
 def one
  flash[:note] = "Hello"
  redirect_to :action => "two"
 end
 def two
  flash.keep(:note)
  redirect_to :action => "three"
 end
 def three
  # flash[:note] => "Hello"
 render
 end
end

Filters
class BlogController < ApplicationController
 before_filter :authorize, :only => [ :delete, :edit_comment ]
 after_filter :log_access, :except => :rss
 # …
before_filter可用來替換全局頁面中的字符,如{$title}
也可用來給頁面 Zlib壓縮
Around Filters可用來計算action用時

Verification
class BlogController < ApplicationController
 verify :only => :post_comment,
 :session => :user_id,
 :add_flash => { :note =>"You must log in to comment"},
 :redirect_to => :index
 # …
使用範圍:
:only =>:name or [ :name, ... ]
:except =>:name or [ :name, ... ]
通過條件:
:flash =>:key or [ :key,... ]
:method =>:symbol or [ :symbol, ... ](:get, :post, :head, or :delete)
:params =>:key or [ :key,... ]
:session =>:key or [ :key,... ]
反應:
:add_flash =>hash
:redirect_to =>params

 GET Requests
 GET用來獲取信息
 POST用來傳輸更改數據庫的信息
 <%= link_to ‘刪除評論’, { :action => ‘com_destroy’, :id => comment },
     :confirm =>"你確定要刪除這則評論嗎?",
     :post => true %>
使用forms和buttons

發表在 Ruby on Rails | 留下評論

更多Ruby on Rails的Active Record

Callbacks
class User < ActiveRecord::Base
 before_destroy :dont_destroy_dave
 def dont_destroy_dave
  raise "Can’t destroy dave" if name == ‘dave’
 end
end

class Order < ActiveRecord::Base
 # ..
 def before_save
  self.payment_due ||= Time.now + 30.days
 end
end

class Order < ActiveRecord::Base
 before_validation :normalize_credit_card_number
 after_create do |order|
  logger.info "Order #{order.id} created"
 end
 protected
 def normalize_credit_card_number
  self.cc_number.gsub!(/-\w/, ”)
 end
end

Callback Objects 全局可以訪問
class CreditCardCallbacks
 # Normalize the credit card number
 def before_validation(model)
  model.cc_number.gsub!(/-\w/,”)
 end
end

class Order < ActiveRecord::Base
 before_validation CreditCardCallbacks.new
 # …
end
 Subscription < ActiveRecord::Base
class
 before_validation CreditCardCallbacks.new
 # …
end
可用於數據加密

格式化數據類型:
class LineItem < ActiveRecord::Base
 def total_price
  Float(read_attribute("total_price"))
 end
end

數量單位轉換:
class ProductData < ActiveRecord::Base
 CUBITS_TO_INCHES = 18
 def length
  read_attribute("length") * CUBITS_TO_INCHES
 end
 
 def length=(inches)
  write_attribute("length", Float(inches) / CUBITS_TO_INCHES)
 end
end

find_by_sql沒有讀取PK時不能保存的實例:
result = LineItem.find_by_sql("select quantity from line_items")
result.each do |li|
 li.quantity += 2
 li.save
end

有特殊功能的段名
created_at, created_on, updated_at, updated_on
 ror自動更新的時間段
 修改ActiveRecord::Base.default_timezone = :utc參數更改變時區
lock_version
type
 (employee, manager, person..)
id
XXX_id (FK)
xxx_count
position (acts_as_list)
partent_id (acts_as_tree)

發表在 Ruby on Rails | 留下評論

Ruby on Rails 的檢驗方法(Validation Helpers)大全

可以自定義validate(), 這個方法在每次保存數據時都會被調用.
如:
def validate
 if name.blank? && email.blank?
  errors.add_to_base("You must specify a name or an email address")
 end
end
同時也可以自定義 validate_on_create(), validate_on_update()方法.
 valid?()方法可以隨時調用,用來測試數據是否能通過校驗
返回的錯誤信息可用 error_messages_for(model)方法顯示.
如:<%= error_messages_for ‘article’ %>

校驗大全:
validates_acceptance_of
 指定checkbox應該選中. (如:(*)我同意條款)
 用法:validates_acceptance_of attr… [ options... ]
 參數:message text  默認:“must be accepted.”
   :on :save, :create, or :update
 實例:
 class Order < ActiveRecord::Base
  validates_acceptance_of :terms,
              :message => "Please accept the terms to proceed"
 end

validates_associated
 查驗指定的object.
 用法:validates_associated name… [ options... ]
 參數:message text 默認: is “is invalid.”
   :on :save, :create, or :update
 實例:
 class Order < ActiveRecord::Base
  has_many :line_items
  belongs_to :user
  validates_associated :line_items,
            :message => "are messed up"
  validates_associated :user
 end

validates_confirmation_of
 數據重校
 用法:validates_confirmation_of attr… [ options... ]
 參數:message text 默認 “doesn’t match confirmation.”
   :on :save, :create, or :update
 實例:
 對密碼錶:
 <%= password_field "user", "password" %><br />
 <%= password_field "user", "password_confirmation" %><br />
 #第二表名為xxxx_confirmation
 class User < ActiveRecord::Base
  validates_confirmation_of :password
 end

validates_each
 使用block檢驗一個或一個以上參數.
 用法:validates_each attr… [ options... ] { |model, attr, value| … }
 參數:allow_nil boolean 設為true時跳過nil對象.
   :on :save, :create, or :update
 實例:
 class User < ActiveRecord::Base
  validates_each :name, :email do |model, attr, value|
   if value =~ /groucho|harpo|chico/i
    model.errors.add(attr,"You can’t be serious, #{value}")
   end
  end
 end

validates_exclusion_of
 確定被檢對象不包括指定數據
 用法:validates_exclusion_of attr…, :in => enum [ options... ]
 #enum指一切可用include?()判斷的範圍.
 參數:allow_nil 設為true將直接跳過nil對象.
   :in (or :within) enumerable
   :message text 默認為: “is not included in the list.”
   :on :save, :create, or :update
 實例:
 class User < ActiveRecord::Base
  validates_exclusion_of :genre,
            :in => %w{ polka twostep foxtrot },
            :message =>"no wild music allowed"
  validates_exclusion_of :age,
             :in => 13..19,
             :message =>"cannot be a teenager"
 end

validates_inclusion_of
 確認對象包括在指定範圍
 用法:validates_inclusion_of attr…, :in => enum [ options... ]
 參數:allow_nil 設為true直接跳過nil對象
   :in (or :within) enumerable An enumerable object.
   :message text 默認:“is not included in the list.”
   :on :save, :create, or :update
 實例:
 class User < ActiveRecord::Base
  validates_inclusion_of :gender,
            :in => %w{ male female },
            :message =>"should be ‘male’ or ‘female’"
  validates_inclusion_of :age,
            :in => 0..130,
            :message =>"should be between 0 and 130"
 end

validates_format_of
 用正則檢驗對象
 用法:validates_format_of attr…, :with => regexp [ options... ]
 參數:message text 默認為: “is invalid.”
   :on :save, :create, or :update
   :with 正則表達式
 實例:
 class User < ActiveRecord::Base
  validates_format_of :length, :with => /^\d+(in|cm)/
 end

validates_length_of
 檢查對象長度
 用法:validates_length_of attr…, [ options... ]
 參數:in (or :within) range
   :is integer
   :minimum integer
   :maximum integer
   :message text 默認文字會根據參數變動,可使用%d 取代確定的最大,最小或指定數據.
   :on :save, :create, or :update
   :too_long text 當使用了 :maximum後的 :message
   :too_short text ( :minimum )
   :wrong_length ( :is)
 實例:
 class User < ActiveRecord::Base
  validates_length_of :name, :maximum => 50
  validates_length_of :password, :in => 6..20
  validates_length_of :address, :minimum => 10,
                :message =>"seems too short"
 end

validates_numericality_of
 檢驗對象是否為數值
 用法:validates_numericality_of attr… [ options... ]
 參數:message text 默認 “is not a number.”
   :on :save, :create, or :update
   :only_integer
 實例:
 class User < ActiveRecord::Base
  validates_numericality_of :height_in_meters
  validates_numericality_of :age, :only_integer => true
 end

validates_presence_of
 檢驗對象是否為空
 用法:validates_presence_of attr… [ options... ]
 參數:message text 默認:“can’t be empty.”
   :on :save, :create, or :update
 實例:
 class User < ActiveRecord::Base
  validates_presence_of :name, :address
 end

validates_uniqueness_of
 檢驗對象是否不重複
 用法:validates_uniqueness_of attr… [ options... ]
 參數:message text 默認: “has already been taken.”
   :on :save, :create, or :update
   :scope attr 指定範圍
 實例:
 class User < ActiveRecord::Base
  validates_uniqueness_of :name
 end

 class User < ActiveRecord::Base
  validates_uniqueness_of :name, :scope =>"group_id"
 end
 #指定在同一group_id的條件下不重複.

常用正則:

E-Mail地址格式:
validates_format_of     :email,
                        :with       => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
                        :message    => ‘email must be valid’

網址格式:
validates_uri_existence_of :url, :with =>
        /(^$)|(^(http|https)://[a-z0-9] ([-.]{1}[a-z0-9] )*.[a-z]{2,5}(([0-9]{1,5})?/.*)?$)/ix

 

發表在 Ruby on Rails | 留下評論

Ruby on Rails 的Active Record之Acts As List和Acts As Tree

Acts As List實例:
在子表中加parent_id和position

定義:
class Parent < ActiveRecord::Base
 has_many :children, :order => :position
end
class

Child < ActiveRecord::Base
 belongs_to :parent
 acts_as_list :scope => :parent_id
#:scope 指定在parent_id相同範圍內.
end

向表中添加數據:
parent = Parent.new
%w{ One Two Three Four}.each do |name|
 parent.children.create(:name => name)
end
parent.save

定義顯示方法:
def display_children(parent)
 puts parent.children.map {|child| child.name }.join(", ")
end

調用實例:
display_children(parent) #=> One, Two, Three, Four

puts parent.children[0].first? #=> true
#使用last?(), first?()判斷行位置

two = parent.children[1]
puts two.lower_item.name  #=> Three
puts two.higher_item.name #=> One
#使用.lower_item訪問下一行數據, 使用.higher_item訪問上一行數據,使用

parent.children[0].move_lower
parent.reload

display_children(parent) #=> Two, One, Three, Four
#使用mover_lower(), mover_higher()可移動行數據
#使用parent.reload刷新數據

parent.children[2].move_to_top
parent.reload
display_children(parent) #=> Three, Two, One, Four
#使用move_to_top(), move_to_bottom()移動行數據

parent.children[2].destroy
parent.reload
display_children(parent) #=> Three, Two, Four
#一行數據刪除後其下面的行會自動跟上

Acts As Tree實例:
向子表新增parent_id (int), 並將設定為表內關聯鍵

定義方法:
class Category < ActiveRecord::Base
 acts_as_tree :order =>"name"
end

向表格添加數據:
root = Category.create(:name => "Books")
fiction = root.children.create(:name =>"Fiction")
non_fiction = root.children.create(:name => "Non Fiction")

non_fiction.children.create(:name =>"Computers")
non_fiction.children.create(:name =>"Science")
non_fiction.children.create(:name =>"Art History")

fiction.children.create(:name =>"Mystery")
fiction.children.create(:name => "Romance")
fiction.children.create(:name =>"Science Fiction")

定義顯示方法:
def display_children(parent)
 puts parent.children.map {|child| child.name }.join(", ")
end

調用實例:
display_children(root) # Fiction, Non Fiction

sub_category = root.children.first
puts sub_category.children.size #=> 3
display_children(sub_category) #=> Mystery, Romance, Science Fiction

non_fiction = root.children.find(:first, :conditions => "name = ‘Non Fiction’")

display_children(non_fiction) #=> Art History, Computers, Science

puts non_fiction.parent.name #=> Books

:counter_cache => true+children_count段也可以在這裡使用.用來自動計算子錶行數.

發表在 Ruby on Rails | 一條評論