æå¹ãªã¦ã¼ã¶ã¼ã ããfindããã¡ã½ããfind_activeãå®ç¾©ãã¦ã¿ã
ã¢ãã«ã以ä¸ã®ããã«ãªã£ã¦ãã¦ãåé¤ãã©ã°ã¨æªè¡¨ç¤ºãã©ã°ã®ï¼ã¤ãããã¨ãã¾ãã
class CreateUsers < ActiveRecord::Migration def self.up create_table "users", :force => true do |t| t.column :login, :string t.column :email, :string t.column :age, :integer t.column :deleted, :boolean, :default => false t.column :hidden, :boolean, :default => false end end def self.down drop_table "users" end end
ãããªæãã®ã¨ãã«æå¹ãªã¦ã¼ã¶ã¼ã®ä¸è¦§ãåºãã«ã¯
users=User.find(:all,:conditions=>["deleted = ? and hidden = ?",false,false])
ã¨ãã£ã¦ããã¾ãããã
ãã§ããã¡ãã¡:conditionsãæ¸ãã®ã¯ãã£ã¦ãããã¼ã£ã¦äººã¯
User.with_scope(:find=>{:conditions=>["deleted = ? and hidden = ?",false,false]}) do users=User.find(:all) end
ã¨ãã£ã¦æ¸ãããã§ãã
ãã§ããç§ã¯with_scopeããæ¸ããããªãã®ã§ãã
findã®ä»£ããã«find_activeã使ãããã®ã§ãã
ããã§ãå®ç¾©
class User < ActiveRecord::Base def self.find_active(*value) User.with_scope(:find=>{:conditions=>["deleted = ? and hidden = ?",false,false]}) do User.find(*value) end end end
ãã®ããã«å®ç¾©ãã¦ããã¨ã
users=User.find_active(:all) users=User.find_active(:all,:conditions=>["age = 30"]) user=User.find_active(:first,:conditions=>["login = taro"])
ã¨ãããµãã«findã¨åããªãã·ã§ã³ã使ãã¦ãã¾ãã¾ãã
使ããããã§ããã