終於算是在今天晚上完成了整個 OneBook 了
至於基本的 CRUD , 目前就只用了 CRD 吧
還有就是習得了算是暫時的 One to Many 的基本用法
記錄一下
關於自定的 YAML 的使用和自動載入
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 1. 先在 conf/initialzers 中新增一個檔案,這裡用 "load_config.rb" 2. 之後編輯並加入 ----------------- APP_CONFIG = YAML.load(File.read(RAILS_ROOT + "/config/gbook.yml")) ----------------- 3. 再來就是建立一個 gbook.yml 在 config 的目錄下,這和上面的目錄要相應 ----------------- title: "一本留言簿" showPage: 2 adminPass: "1234" ----------------- 4. 最後使用的方法就是 <%=APP_CONFIG["title"]%>
(注意,每次改動 gbook.yml 都雖要重新開啟測試的 Web server)
|
還有最重要的是表名要用複數形態表示,而模型 Model 就雖用單數?
1 2 3 4 5 6 7 8
| # 進入 App 的目錄 ruby script/console
# 查詢 reply 的複數 "reply".pluralize
# 查詢 replies 的單數 "replies".singularize
|
另外還有一些是關於 ActiveRecord 的認識,一對多也算是初步用上了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| message.rb ---------- class Message < ActiveRecord::Base has_many :replies, :foreign_key => 'message_id', :order => "adddate DESC", :dependent => :destroy end
reply.rb ---------- class Reply < ActiveRecord::Base belongs_to :messages end
@message.replies
|
另外的一些參考
加強 Active Record 的關連性
http://lightyror.thegiive.net/2007/01/active-record.html
Ruby 對單單和複數處理相關 API
http://api.rubyonrails.org/classes/Inflector.html
ActiveRecord memo(2)
http://ilakeruby.blogspot.com/2009/03/activerecord-memo2.html