精華區beta Ruby 關於我們 聯絡資訊
出自我的 Blog http://lightyror.blogspot.com/2006/10/active-record-find.html Jamis Buck 提出了一個好範例 教導我們如何簡單的製作出一個可以隨機取出 entry 的 Active Record class Thing < ActiveRecord::Base def self.find(*args) if args.first.to_s == "random" ids = connection.select_all("SELECT id FROM things") super(ids[rand(ids.length)]["id"].to_i) else super end end end 之後你就可以這樣使用 Thing.find('random') 他就會隨機取出任一個 entry 有了這個範例,其實我們可以依照您的需求,特別訂做出多采多姿的find -- lighty RoR 是一個介紹 lighttpd , SQLite , Ruby and Rails 的 Blog http://lightyror.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.170.38.220