ユーザ用ツール

サイト用ツール


rails:active_record

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
rails:active_record [2024/01/10 09:54] – [テーブル結合] nullponrails:active_record [2024/01/10 14:30] (現在) nullpon
行 71: 行 71:
  
 ===== nemed_scope ===== ===== nemed_scope =====
-(rails 2.1 or later) 
  
 よくある検索条件をメソッド化できる。 よくある検索条件をメソッド化できる。
 <code rubyt> <code rubyt>
 User < ActiveRecord::Base User < ActiveRecord::Base
-  named_scope :active, :conditions => { "state" => "active" } +  scope :active, where(state: "active") 
-  named_scope :teenager, :conditions => {"age=13..19 } +  scope :teenager, where("age >= 13 and age <= 19") 
-  named_scope :male :condtions => {"sex" => } +  scope :male, where(gender: 1) 
-  named_scope :age, lambda{ |x{ :conditions => ["age = ?", x] }}+  scope :age, -> x { where(age: x}
 end end
 </code> </code>
行 147: 行 146:
  
 検索時は 検索時は
-  LimitedItem.find(:all+  LimitedItem.all 
 とすれば、 とすれば、
   select * from items where type = "LimitedItem";   select * from items where type = "LimitedItem";
行 160: 行 159:
 class ItemsController class ItemsController
   def index   def index
-    @items = Item.find(:all)+    @items = Item.all
   end   end
 end end
rails/active_record.1704880466.txt.gz · 最終更新: 2024/01/10 09:54 by nullpon