rails查詢學習筆記


2011-08-30 13:05:33|  分類: Ruby|字號 訂閱

教程原文http://guides.rubyonrails.org/active_record_querying.html 
1、獲取數據 
.獲取第一條、最後一條記錄 
Ruby代碼  收藏代碼
  1. Model.first  
  2. Model.first(options)  
  3. Model.find(:first, options)  
  4.   
  5. Model.last  
  6. Model.last(options)  
  7. Model.find(:last, options)  


.通過id獲取記錄 
Ruby代碼  收藏代碼
  1. Model.find(1, 10, options)  
  2. Model.find([1, 10], options)  


.find all 
Ruby代碼  收藏代碼
  1. Model.all(options)  



.對一組數據進行相同操作 
Ruby代碼  收藏代碼
  1. User.all.each do |user|  
  2.     NewsLetter.weekly_deliver(user)  
  3. end  

如果表記錄數比較大,這種方式比較耗資源,因爲它會一次載入整個表的數據。改用以下這種方式,它每次只載入1000行,然後逐步yield完整個表 
Ruby代碼  收藏代碼
  1. User.find_each do |user|  
  2.     NewsLetter.weekly_deliver(user)  
  3. end  

自定義方式,find_each接受和find同樣的options 
Ruby代碼  收藏代碼
  1. User.find_each(:batch_size => 5000, :start => 2000) do |user|  
  2.     NewsLetter.weekly_deliver(user)  
  3. end  

find_in_batches,和find_each相似,但它yield時傳遞的是model對象數組,而不是單個model對象 
Ruby代碼  收藏代碼
  1. Invoice.find_in_batches(:include => :invoice_linesdo |invoices|  
  2.     export.add_invoices(invoices)  
  3. end  


2、查詢條件 
通過替換?來傳遞條件值,可避免SQL注入 
Ruby代碼  收藏代碼
  1. Client.first(:conditions => ["orders_count = ?", params[:orders])  


symbol佔位條件 
Ruby代碼  收藏代碼
  1. Client.all(:conditions => ["created_at >= :start_date AND created_at <= :end_date", {:start_date => params[:start_date], :end_date => params[:end_date] }])  



範圍條件 in(集合) 
Ruby代碼  收藏代碼
  1. Client.all(:conditions => ["created_at IN (?)", (params[:start_date].to_date)..(params[:end_date].to_date])  

生成sql 
Sql代碼  收藏代碼
  1. SELECT * FROM users WHERE (created_at IN ('2007-12-31','2008-01-01','2008-01-02','2008-01-03','2008-01-04','2008-01-05''2008-01-06','2008-01-07','2008-01-08'))  

如果要生成日期時間,再加上.to_time 
params[:start_date].to_date.to_time,生成2007-12-01 00:00:00格式 

有上數據庫會在以上條件中報錯,如Mysql會報查詢語句過長的錯誤,此時可以改成created_at > ? AND created_at < ?的形式 

Hash條件 
Ruby代碼  收藏代碼
  1. Client.all(:conditions => {:locked => true })   


帶範圍條件 
Ruby代碼  收藏代碼
  1. Client.all(:conditons => {:created => (Time.now.midnight - 1.day)..Time.now.midnight})  

生成sql 
Sql代碼  收藏代碼
  1. SELECT * FROM clients WHERE (clients.created_at BETWEEN '2008-12-21 00:00:00' AND '2008-12-22 00:00:00')  


集合條件 
Ruby代碼  收藏代碼
  1. Client.all(:conditons => {:orders_count => [1,3,5])  

生成sql 
Sql代碼  收藏代碼
  1. SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5))   


3、查詢選項 
排序 
Ruby代碼  收藏代碼
  1. #單個排序  
  2. Client.all(:order => "created_at ASC")  
  3. #多個排序  
  4. Client.all(:order => "orders_count ASC, created_at DESC")  


返回指定字段 
Ruby代碼  收藏代碼
  1. Client.all(:select => "viewable_by, locked")  
  2. #使用函數  
  3. Client.all(:select => "DISTINCT(name)")  


限定和偏移Limit and Offset 
Ruby代碼  收藏代碼
  1. Client.all(:limit => 5)  
  2. #生成  
  3. SELECT * FROM clients LIMIT 5  
  4. Client.all(:limit => 5, :offset => 5)  
  5. #生成  
  6. SELECT * FROM clients LIMIT 5, 5  


Group分組 
Ruby代碼  收藏代碼
  1. Order.all(:group => "date(created_at)":order => "created_at")  

生成sql 
Sql代碼  收藏代碼
  1. SELECT * FROM orders GROUP BY date(created_at)  


Having 
Ruby代碼  收藏代碼
  1. Order.all(:group => "date(created_at)":having => ["created_at > ?", 1.month.ago)  

生成sql 
Sql代碼  收藏代碼
  1. SELECT * FROM orders GROUP BY date(created_at) HAVING created_at > '2009-01-15'   


只讀 
Ruby代碼  收藏代碼
  1. client = Client.first(:readonly => true)  
  2. client.locked = false  
  3. client.save  
  4. #對只讀對象進行保存將會觸發ActiveRecord::ReadOnlyRecord異常  


更新時鎖定記錄 
樂觀鎖Optimistic Locking 
爲使用樂觀鎖,須在表裏建一個lock_version的字段,每次更新記錄時,ActiveRecord自動遞增lock_version的值, 
Ruby代碼  收藏代碼
  1. c1 = Client.find(1) c2 = Client.find(1) c1.name = "Michael" c1.save c2.name = "should fail" c2.save # Raises a ActiveRecord::StaleObjectError   

備註:You must ensure that your database schema defaults the lock_version column to 0. 

This behavior can be turned off by setting ActiveRecord::Base.lock_optimistically = false. 

指定樂觀鎖字段名 
Ruby代碼  收藏代碼
  1. class Client < ActiveRecord::Base set_locking_column :lock_client_column end   



悲觀鎖Pessimistic Locking 
悲觀鎖定由數據庫直接提供 
Ruby代碼  收藏代碼
  1. Item.transaction do   
  2.     i = Item.first(:lock => true)  
  3.     i.name = 'Jones'  
  4.     i.save  
  5. end  

Mysql執行返回 
SQL (0.2ms) BEGIN Item Load (0.3ms) SELECT * FROM `items` LIMIT 1 FOR UPDATE Item Update (0.4ms) UPDATE `items` SET `updated_at` = '2009-02-07 18:05:56', `name` = 'Jones' WHERE `id` = 1 SQL (0.8ms) COMMIT 

爲特定數據庫加入原始的lock聲明 
爲Mysql的鎖定聲明爲共享模式,即鎖定時仍然可讀 
Item.transaction do  i = Item.find(1, :lock => "LOCK IN SHARE MODE")  i.increment!(:views) end 

4、關聯表 
Ruby代碼  收藏代碼
  1. Client.all(:joins => "LEFT OUTER JOIN address ON addresses.client_id = clients.id')  

生成sql 
Sql代碼  收藏代碼
  1. SELECT clients.* FROM clients LEFT OUTER JOIN addresses ON addresses.client_id = clients.id   


使用Array、Hash、Named Associations關聯表 
有如下model 
Ruby代碼  收藏代碼
  1. class Category < ActiveRecord::Base   
  2.     has_many :posts   
  3. end   
  4. class Post < ActiveRecord::Base   
  5.     belongs_to :category    
  6.     has_many :comments        
  7.     has_many :tags   
  8. end   
  9. class Comments <ActiveRecord::Base   
  10.     belongs_to :post    
  11.     has_one :guest   
  12. end   
  13. class Guest < ActiveRecord::Base   
  14.     belongs_to :comment   
  15. end   



Ruby代碼  收藏代碼
  1. #關聯一個關係  
  2. Category.all :joins => :posts  
  3. #關聯多個關係  
  4. Post.all :joins => [:category:comments]   
  5. #嵌套關聯  
  6. Category.all :joins => {:posts => [{:comments => :guest}, :tags]}   


爲關聯查詢結果設定條件 
Ruby代碼  收藏代碼
  1. time_range = (Time.now.midnight - 1.day)..Time.now.midnight Client.all :joins => :orders:conditions => {'orders.created_at' => time_ran  
  2. #或者  
  3. time_range = (Time.now.midnight - 1.day)..Time.now.midnight Client.all :joins => :orders:conditions => {:orders => {:created_at => time_range}}   


5、優化載入 
以下代碼,需要執行1 + 10次sql 
Ruby代碼  收藏代碼
  1. clients = Client.all(:limit => 10) clients.each do |client|   
  2.     puts client.address.postcode   
  3. end   

優化: 
Ruby代碼  收藏代碼
  1. clients = Client.all(:include => :address:limit => 10)   
  2. clients.each do |client|   
  3.     puts client.address.postcode   
  4. end   


一次性載入post的所有分類和評論 
Ruby代碼  收藏代碼
  1. Post.all :include => [:category:comments]   


載入category爲1的所有post和cooment及tag 
Ruby代碼  收藏代碼
  1. Category.find 1, :include => {:posts => [{:comments => :guest}, :tags]}   


6、動態查詢 
Ruby代碼  收藏代碼
  1. Client.find_by_name("Ryan")  
  2. Client.find_all_by_name("Ryan")  
  3.   
  4. #!方法,沒有記錄時拋出ActiveRecord::RecordNotFound異常  
  5. Client.find_by_name!("Ryan")  
  6.   
  7. #查詢多個字段  
  8. Client.find_by_name_and_locked("Ryan"true)  
  9.   
  10. #查詢不到時就創建並保存  
  11. Client.find_or_create_by_name(params[:name])  
  12. #查詢不到時創建一個實例,但不保存  
  13. Client.find_or_initialize_by_name('Ryan')  


7、find_by_sql 
Ruby代碼  收藏代碼
  1. Client.find_by_sql("SELECT * FROM clients INNER JOIN orders ON clients.id = orders.client_id ORDER clients.created_at desc")   


8、select_all 
和find_by_sql類似,但不會用model實例化返回記錄,你會得到一個hash數組 
Ruby代碼  收藏代碼
  1. Client.connection.select_all("SELECT * FROM clients WHERE id = '1'")   


9、判斷記錄是否存在 
Ruby代碼  收藏代碼
  1. #通過id來查詢  
  2. Client.exists?(1)  
  3.   
  4. Client.exists?(1, 2, 3)  
  5. #or  
  6. Client.exists?([1,2,3])  
  7.   
  8. #通過其他條件來查詢  
  9. Client.exists?(:conditions => "first_name = 'Ryan'")  
  10.   
  11. #沒有參數時,則:表是空的 ? false : true  
  12. Client.exists?  


10、計算 

Ruby代碼  收藏代碼
  1. #求結果集條數  
  2. Client.count(:conditons => "first_name = 'Ryan'")  
  3.   
  4. #求某個字段非空白的條數  
  5. Client.count(:age)  
  6.   
  7. #平均值  
  8. Client.average("orders_count")  
  9.   
  10. #求最小值  
  11. Client.minimum("age")  
  12.   
  13. #求最大值  
  14. Client.maximum("age")  
  15.   
  16. #求和  
  17. Client.sum("orders_count")  
發佈了32 篇原創文章 · 獲贊 11 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章