分類目錄歸檔:Ruby on Rails

US States List for Ruby on Rails

def select_state_for_user
       [["","Select State..."], ["AL","Alabama"],
        ["AK","Alaska"], ["AZ","Arizona"], ["AR","Arkansas"],
        ["CA","California"], ["CO","Colorado"], ["CT","Connecticut"],
        ["DE","Delaware"], ["DC","District of Columbia"],
["FL","Florida"],

        ["GA","Georgia"], ["HI","Hawaii"], ["ID","Idaho"],
["IL","Illinois"],
        ["IN","Indiana"], ["IA","Iowa"], ["KS","Kansas"],
["KY","Kentucky"],
        ["LA","Louisiana"], ["ME","Maine"], ["MD","Maryland"],
        ["MA","Massachusetts"], ["MI","Michigan"], ["MN","Minnesota"],
        ["MS","Mississippi"], ["MO","Missouri"], ["MT","Montana"],
        ["NE","Nebraska"], ["NV","Nevada"], ["NH","New Hampshire"],
["NJ","New
        Jersey"], ["NM","New Mexico"], ["NY","New York"], ["NC","North
        Carolina"], ["ND","North Dakota"], ["OH","Ohio"],
["OK","Oklahoma"],
        ["OR","Oregon"], ["PA","Pennsylvania"], ["RI","Rhode Island"],
        ["SC","South Carolina"], ["SD","South Dakota"],
["TN","Tennessee"],
        ["TX","Texas"], ["UT","Utah"], ["VT","Vermont"],
["VA","Virginia"],
        ["WA","Washington"], ["WV","West Virginia"], ["WI","Wisconsin"],
        ["WY","Wyoming"]]
end

STATES = [
    [ "Alabama", "AL" ],
    [ "Alaska", "AK" ],
    [ "Arizona", "AZ" ],
    [ "Arkansas", "AR" ],
    [ "California", "CA" ],
    [ "Colorado", "CO" ],
    [ "Connecticut", "CT" ],
    [ "Delaware", "DE" ],
    [ "Florida", "FL" ],
    [ "Georgia", "GA" ],
    [ "Hawaii", "HI" ],
    [ "Idaho", "ID" ],
    [ "Illinois", "IL" ],
    [ "Indiana", "IN" ],
    [ "Iowa", "IA" ],
    [ "Kansas", "KS" ],
    [ "Kentucky", "KY" ],
    [ "Louisiana", "LA" ],
    [ "Maine", "ME" ],
    [ "Maryland", "MD" ],
    [ "Massachusetts", "MA" ],
    [ "Michigan", "MI" ],
    [ "Minnesota", "MN" ],
    [ "Mississippi", "MS" ],
    [ "Missouri", "MO" ],
    [ "Montana", "MT" ],
    [ "Nebraska", "NE" ],
    [ "Nevada", "NV" ],
    [ "New Hampshire", "NH" ],
    [ "New Jersey", "NJ" ],
    [ "New Mexico", "NM" ],
    [ "New York", "NY" ],
    [ "North Carolina", "NC" ],
    [ "North Dakota", "ND" ],
    [ "Ohio", "OH" ],
    [ "Oklahoma", "OK" ],
    [ "Oregon", "OR" ],
    [ "Pennsylvania", "PA" ],
    [ "Rhode Island", "RI" ],
    [ "South Carolina", "SC" ],
    [ "South Dakota", "SD" ],
    [ "Tennessee", "TN" ],
    [ "Texas", "TX" ],
    [ "Utah", "UT" ],
    [ "Vermont", "VT" ],
    [ "Virginia", "VA" ],
    [ "Washington", "WA" ],
    [ "West Virginia", "WV" ],
    [ "Wisconsin", "WI" ],
    [ "Wyoming", "WY" ]
  ].freeze

 STATES = [
    [ "", "" ],
    [ "Alabama", "Alabama" ],
    [ "Alaska", "Alaska" ],
    [ "Arizona", "Arizona" ],
    [ "Arkansas", "Arkansas" ],
    [ "California", "California" ],
    [ "Colorado", "Colorado" ],
    [ "Connecticut", "Connecticut" ],
    [ "Delaware", "Delaware" ],
    [ "Florida", "Florida" ],
    [ "Georgia", "Georgia" ],
    [ "Hawaii", "Hawaii" ],
    [ "Idaho", "Idaho" ],
    [ "Illinois", "Illinois" ],
    [ "Indiana", "Indiana" ],
    [ "Iowa", "Iowa" ],
    [ "Kansas", "Kansas" ],
    [ "Kentucky", "Kentucky" ],
    [ "Louisiana", "Louisiana" ],
    [ "Maine", "Maine" ],
    [ "Maryland", "Maryland" ],
    [ "Massachusetts", "Massachusetts" ],
    [ "Michigan", "Michigan" ],
    [ "Minnesota", "Minnesota" ],
    [ "Mississippi", "Mississippi" ],
    [ "Missouri", "Missouri" ],
    [ "Montana", "Montana" ],
    [ "Nebraska", "Nebraska" ],
    [ "Nevada", "Nevada" ],
    [ "New Hampshire", "New Hampshire" ],
    [ "New Jersey", "New Jersey" ],
    [ "New Mexico", "New Mexico" ],
    [ "New York", "New York" ],
    [ "North Carolina", "North Carolina" ],
    [ "North Dakota", "North Dakota" ],
    [ "Ohio", "Ohio" ],
    [ "Oklahoma", "Oklahoma" ],
    [ "Oregon", "Oregon" ],
    [ "Pennsylvania", "Pennsylvania" ],
    [ "Rhode Island", "Rhode Island" ],
    [ "South Carolina", "South Carolina" ],
    [ "South Dakota", "South Dakota" ],
    [ "Tennessee", "Tennessee" ],
    [ "Texas", "Texas" ],
    [ "Utah", "Utah" ],
    [ "Vermont", "Vermont" ],
    [ "Virginia", "Virginia" ],
    [ "Washington", "Washington" ],
    [ "West Virginia", "West Virginia" ],
    [ "Wisconsin", "Wisconsin" ],
    [ "Wyoming", "Wyoming" ]
  ]

發表在 Ruby on Rails | 留下評論

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_idposition

定義:
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 | 一條評論

Ruby on Rails數據表格關係

一對一關係實例: one-to-one
class Order < ActiveRecord::Base
has_one :paid_order,
    :class_name =>"Order",
    :foreign_key => "order_id",
    :conditions => "paid_on is not null"
在表格上加order_id (表格名單數_id)
class Invoice < ActiveRecord::Base
belongs_to :order

可選參數:class_name, :foreign_key,  :order, :conditions
:dependent => true #刪除主錶行時同時刪除子行
自定義的order用法:
class Customer < ActiveRecord::Base
has_many :orders
has_one :most_recent_order,
    :class_name => ‘Order’,
    :order => ‘created_at DESC’
end

主.從 將被保存
an_invoice = Invoice.new(…)
order.invoice = an_invoice # invoice被保存
從.主 將要手動保存

新增加的方法:
product(force_reload=false)
product=(obj)
build_product(attributes={})
create_product(attributes={})

一對多關係(one-to-many)
class Order < ActiveRecord::Base
has_many :line_items
可選參數除了上面的,還有:exclusively_dependent, :finder_sql,:counter_sql
:exclusively_dependent 在子行沒有其它表格瓜葛的情況下使用, 加快處理速度.
:finder_sql的使用實例:
class Order < ActiveRecord::Base
has_many :rails_line_items,
     :class_name => "LineItem",
     :finder_sql => "select l. * from line_items l, products p " +
     " where l.product_id = p.id " +
     " and p.title like ‘%rails%’"
end
order的用法:
class Order < ActiveRecord::Base
has_many :line_items,
   :order =>"quantity, unit_price DESC"
end

主.從 將被保存
an_invoice = Invoice.new(…)
order.invoices <<an_invoice # invoice

class LineItem < ActiveRecord::Base
belongs_to :order
. . .

has_many後可以引用集合:
order = Order.find(123)
total = 0.0
order.line_items.each do |li|
 total += li.quantity * li.unit_price
end

新增加的方法:
orders(force_reload=false)
orders <<order
orders.push(order1, …)
orders.delete(order1, …)
orders.clear
orders.find(options…)
orders.build(attributes={})
orders.create(attributes={})

多對多關係(many-to-many):
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
. . .
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
. . .

要創建一個中間表格:
categories_products
兩表格名字複數形式相聯, 排字母前後排序

表格內連關聯鍵

預載子表
用:include將子表內容加入內存,提高查詢速度, 但損耗內存:
for post in Post.find(:all, :include => [:author, :comments])
 puts "Post: #{post.title}"
 puts "Written by: #{post.author.name}"
 puts "Last comment on: #{post.comments.first.created_on}"
end

自動計算子錶行數
 belongs_to加參數:counter_cache => true
 數據庫加 子表格名(複數)_count 段, 並加 :default=>0參數.
 然後用 .size可以讀取子錶行數.
刷新數據讀法:product.line_items(:refresh).size

發表在 Ruby on Rails | 2 條評論

Ruby on Rails 數據庫Migration操作語句實例

def self.up     # db schema更新到下一版本
  create_table :table, :force => true do |t| #創建表格
    t.column :name, :string
    t.column :age, :integer, { :default => 42 }
    t.column :description, :text
    # :string, :text, :integer, :float, :datetime, :timestamp, :time, :date,
    # :binary, :boolean
  end
  add_column :table, :column, :type #添加段

  rename_column :table, :old_name, :new_name #修改段名
  change_column :table, :column, :new_type #修改段數據類型
  execute "SQL語句"
  add_index :table, :column, :unique => true, :name => ‘some_name’  #添加索引
  add_index :table, [ :column1, :column2 ]
end

def self.down   # 撤消操作
  rename_column :table, :new_name, :old_name
  remove_column :table, :column
  drop_table :table #刪除表格
  remove_index :table, :column
end

發表在 Ruby on Rails | 留下評論

Ruby on Rails HTML表單語句大全

表單開始標籤:
<%= form_tag { :action => :save }, { :method => :post } %>
Use :multipart => true to define a Mime-Multipart form (for file uploads)
表單結束標籤:
<%= end_form_tag %>

文本框 Text fields
<%= text_field :modelname, :attribute_name, options  %>
生成:
<input type="text" name="modelname[attribute_name]" id="attributename" />

實例:

text_field "post", "title", "size" => 20
    <input  type="text" id="post_title" name="post[title]"
            size="20" value="#{@post.title}" />

隱藏框:
<%= hidden_field … %>

密碼框:
<%= password_field … %>

文件框
<%= file_field … %>

Rails Textarea框
<%= text_area … %>
實例:
text_area "post", "body", "cols" => 20, "rows" => 40
    <textarea cols="20" rows="40" id="post_body" name="post[body]">
        #{@post.body}
    </textarea>

單選框 Radio Buttons
<%= radio_button :modelname, :attribute, :tag_value, options %>
實例:
radio_button "post", "category", "rails"
radio_button "post", "category", "java"
    <input type="radio" id="post_category" name="post[category]" value="rails"
           checked="checked" />
    <input type="radio" id="post_category" name="post[category]" value="java" />

多選框 Check Box
<%= check_box :modelname, :attribute, options, on_value, off_value %>
實例
check_box "post", "validated"   # post.validated? returns 1 or 0
    <input type="checkbox" id="post_validate" name="post[validated]"
           value="1" checked="checked" />
    <input name="post[validated]" type="hidden" value="0" />

check_box "puppy", "gooddog", {}, "yes", "no"
    <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
    <input name="puppy[gooddog]" type="hidden" value="no" />

<%= select :variable, :attribute, choices, options, html_options %>

下拉菜單框 Select Menu
select  "post",        
        "person_id",   
        Person.find_all.collect {|p| [ p.name, p.id ] },
        { :include_blank => true }

 <select name="post[person_id]">
   <option></option>
   <option value="1" selected="selected">David</option>
   <option value="2">Sam</option>
   <option value="3">Tobias</option>
 </select>

Collection Selection
<%= collection_select :variable, :attribute, choices, :id, :value %>

日期選擇框:
<%= date_select :variable, :attribute, options %>
<%= datetime_select :variable, :attribute, options %>
實例:
date_select "post", "written_on"
date_select "user", "birthday", :start_year => 1910
date_select "user", "cc_date", :start_year => 2005,
                               :use_month_numbers => true,
                               :discard_day => true,
                               :order => [:year, :month]

datetime_select "post", "written_on"

發表在 Ruby on Rails | 留下評論

Ruby on Rails數據庫操作

數據庫元被影射成對象
(object-relational mapping (ORM)層)
table<=>class
row<=>object
column<=>object attribute

class和table的命名對應關係實例
Order<=>orders
TaxAgency<=>tax_agencies
Person<=>people

複數形式關閉方法config/environment.rb:
ActiveRecord::Base.pluralize_table_names = false
自定義表格名稱方法:
class Sheep < ActiveRecord::Base
 set_table_name "sheep"
end

Ruyb數據類型和SQL數據類型對應關係表
int,integer<=>Fixnum
float,double,decimal,numeric<=>Float
interval,date<=>Date
datetime,time<=>Time
char,varchar,string,clob,blob,text<=>String
boolean<=>see text…

訪問屬性(數據庫列):
account[:balance] #=> 返回當前值
account[:balance] = 0.0 #=> 指定數值

修正數據庫列的取值範圍的方法:
class Account < ActiveRecord::Base
 def balance=(value)
  raise BalanceTooLow if value < MINIMUM_LEVEL
  self[:balance] = value
 end
end

訪問屬性(數據庫列)更方便的方法:
account.balance #=> 返回當前值
account.balance = 0.0 #=> 指定數值

以上方式得到的數據庫數據將是ruby按自身的數據類型格式化好的,如果要得到原始數據,可用以下形式代碼:
account.balance_before_type_cast #=> "123.4", 字符串
account.release_date_before_type_cast #=> "20050301"

是非屬性
在ruby中只有falsenil才被判斷為false

數據庫主鍵(Primary Keys)
Ruby on Rails默認以id為主鍵

自定義主鍵的方法:
class BadBook < ActiveRecord::Base
 set_primary_key "isbn"
end

數據創建,讀取,更新和刪除(CRUD:Create, Read, Update, Delete)

創建新數據

實例:
an_order = Order.new
an_order.name ="Dave Thomas"
an_order.save #在save()之前所有數據只存在內存中

用以下方式可以減少產生一個an_order變量:
Order.new do |o|
 o.name = "Dave Thomas"
 # . . .
 o.save
end

當數據來自HTML表單時,可以考慮用以下方式:
an_order = Order.new(
 :name =>"Dave Thomas",
#…
)
an_order.save

使用create()代換new()可直接保存到數據庫,省去an_order.save:
an_order = Order.create(
 :name => "Dave Thomas")

可以使用hash同時保存多組數據:
 orders = Order.create(
  [ { :name =>"Dave Thomas" },
   { :name =>"Andy Hunt" } ] )

new()或create()也可以直接接參數:
order = Order.create(params)

讀取數據
an_order = Order.find(27)
# 直接找出id = 27的數據,可以同時讀取多個object.
an_order = Order.find_by_id(27)
# 效果類似,但一次只能讀一個,在找不到符合的object時返回nil,而不是錯誤。
# 從一個表單讀取product id列表,然後計算這些商品的總價:
product_list = params[:product_ids]
total = 0.0
Product.find(product_list).each {|prd| total += prd.total}

帶條件的讀取:
pos = Order.find(:all,
:conditions => "name = ‘dave’ and pay_type = ‘po’")

不安全的表單參數傳遞讀取數據庫:
name = params[:name]
# 此方法有被SQL注入方式入侵的風險!!!
pos = Order.find(:all,
:conditions =>"name = ‘#{name}‘ and pay_type = ‘po’")
#注意上面單雙引號的使用及變量的傳遞方法

更安全的方法:
name = params[:name]
pos = Order.find(:all,
:conditions => ["name = ? and pay_type = 'po'", name])

你也可以這樣:
name = params[:name]
pay_type = params[:pay_type]
pos = Order.find(:all,
:conditions => ["name = :name and pay_type = :pay_type",
{:pay_type => pay_type, :name => name}])

終極簡化版:
pos = Order.find(:all,
:conditions => ["name = :name and pay_type = :pay_type", params])

排序和查找第3(?)至13(?)列的方法:
orders = Order.find(:all,
 :conditions =>"name = ‘Dave’",
 :order =>"pay_type, shipped_at DESC",
 :limit => 10
 :offset => 2)

聯合數據表的查找方法(一般用不上):
LineItem.find(:all,
:conditions => "pr.title = ‘Programming Ruby’",
:joins =>"as li inner join products as pr on li.product_id = pr.id")

查找有序一列的方法:
order = Order.find( :first,
 :conditions =>"name = ‘Dave Thomas’",
 :order => "id DESC")

直接使用sql語句的查詢方法:
items = LineItem.find_by_sql("select *, quantity*unit_price as total_price,products.title as title from line_items, products where line_items.product_id = products.id ")
li = items[0]
puts "#{li.title}: #{li.quantity}x#{li.unit_price} => #{li.total_price}"
你可以使用"as".

在這裡你也可以傳遞參數:
Order.find_by_sql(["select * from orders where amount > ?",
params[:amount]])

計算行數
c1 = Order.count
c2 = Order.count(["name = ?", "Dave Thomas"])
c3 = LineItem.count_by_sql("select count(*) from line_items, orders  where line_items.order_id = orders.id and orders.name = ‘Dave Thomas’ ")
puts "Dave在#{c2}個定單里一共定了#{c3} 件商品 (目前定單總數:#{c1})"

動態查詢
order = Order.find_by_name("Dave Thomas")#只查一列
orders = Order.find_all_by_name("Dave Thomas")
order = Order.find_all_by_email(params['email'])

可同時查多個條件,如:
user = User.find_by_name_and_password(name, pw)

重載數據庫
stock = Market.find_by_ticker("RUBY")
loop do
 puts "Price = #{stock.price}"
 sleep 60
 stock.reload
end

更新數據
使用save()
order = Order.find(123)
order.name = "Fred"
order.save

orders = Order.find_by_sql("select id, name, pay_type from orders where id=123")
first = orders[0]
first.name ="Wilma"
first.save

使用update_attribute()
order = Order.find(123)
order.update_attribute(:name,"Barney")
order = Order.find(321)
order.update_attributes(:name => "Barney",
:email =>"barney@bedrock.com")

使用更快捷的update()
order = Order.update(12, :name => "Barney", :email => "barney@bedrock.com")

使用update_all()
result = Product.update_all("price = 1.1*price", "title like ‘%Java%‘")

save()和save!()
save()失敗時返回nil
if order.save
 # 成功
else
 # 保存失敗則…
end

save!()失敗時出錯
begin
 order.save!
rescue RecordInvalid => error
 # 保存失敗RecordInvalid exception
end

數據鎖(防止數據保存撞車)
加段:lock_version int default 0,

刪除數據
delete()刪除
Order.delete(123)
User.delete([2,3,4,5])
Product.delete_all(["price > ?", @expensive_price])

destroy()凍結(在model層面)
order = Order.find_by_name("Dave")
order.destroy
# … order將被凍結

數據庫關係(相關鍵)參看:Ruby on Rails數據表格關係

發表在 Ruby on Rails | 留下評論