Ruby on Railsã®Modelå ã«è¨è¿°ããã¡ã½ããã®åé¡è¡¨
Ruby on Railsã§ï¼åã¢ãã«ã¯ã©ã¹ã«è¨è¿°ãã¹ãã¡ã½ããã®ï¼åé¡æ¸ã¿ä¸è¦§è¡¨ã
ãåã¢ãã«ãã¨ã¯ï¼
- DBã«ã¢ã¯ã»ã¹ããã¯ã©ã¹ã
- ãã®ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãªãã¸ã§ã¯ããã¢ããªå ¨ä½ã§æã¡åããï¼åä¸ã®ã¬ã³ã¼ããæ±ãä¸ã§ä¾¿å©ãªåããããã
ããã«å¯¾ãï¼ã親ã¢ãã«ãï¼ï¼ãµã¼ãã¹å±¤ï¼ã¯ï¼
- æ¥åãæ©è½ã®ãã¸ãã¹ãã¸ãã¯ãªã©ï¼ã³ã³ããã¼ã©ã®åã¢ã¯ã·ã§ã³ããå¼ã³åºãããã²ã¨ã¾ã¨ã¾ãã®å¦çããã©ã³ã¶ã¯ã·ã§ã³ä»ãã§è¨è¿°ããã¯ã©ã¹ã
- æ¥åå¦çãè¨è¿°ããããã«ï¼å種ã®åã¢ãã«ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãçæãã¦åç §ãä¿åãªã©ãè¡ãªããããã¡ããã©ï¼åã¢ãã«ããè¦ãã°ãããã¼ã¸ã£ãã®å½¹å²ããããã³ã³ããã¼ã©ããè¦ãã°ï¼æ©è½ã«é¢ããããµã¼ãã¹ãã§ããã
以ä¸ã¯ï¼
- ï¼ï¼ï¼åã¢ãã«ã®ã¡ã½ããåé¡
- ï¼ï¼ï¼ã¡ã½ããã®æ¸ãåãè¦ç´
ï¼ï¼ï¼åã¢ãã«ã®ã¡ã½ããåé¡è¡¨
åã¢ãã«å ã®å®è£ ã¡ã½ããã®åé¡ |
|||
---|---|---|---|
CRUDã® çºçãã ã¬ã¤ã¤â |
ã¡ã¢ãªä¸ |
DBä¸ |
|
ã¬ã·ã¼ãåé¡â âCRUDåé¡ |
ã¤ã³ã¹ã¿ã³ã¹ ã¡ã½ãã |
ã¤ã³ã¹ã¿ã³ã¹ ã¡ã½ãã |
ã¯ã©ã¹ ã¡ã½ãã |
C (Create) |
new (ã³ã³ã¹ãã©ã¯ã¿) |
save |
create |
R (Read) |
getterã¡ã½ããé¡ |
reload |
find å種åçãã¡ã¤ã³ã |
U (Update) |
setterã¡ã½ããé¡ |
update update_attributes |
find + setter + save find + update |
D (Delete) |
- (ãã¹ãã©ã¯ã¿) |
destroy |
delete destroy delete_all destroy_all |
ã°ã¬ã¼ã¢ã¦ããããé¨åã¯éæ¨å¥¨
6. ã¯ã©ã¹ãã¢ã¸ã¥ã¼ã«
http://www.ruby-lang.org/ja/man/html/...
ã¬ã·ã¼ããã¯ã©ã¹åã§ããã°ã¯ã©ã¹ã¡ã½ããã«ãªã
http://blade.nagaokaut.ac.jp/cgi-bin/...
Rubyã«ã¯æ示çãªãªãã¸ã§ã¯ãç ´æ£ã®æ¹æ³ã¯ç¡ã
ActiveRecord reload(options = nil)
http://ar.rubyonrails.org/classes/Act...
ActiveRecord æ´æ°ç³»
http://d.hatena.ne.jp/elm200/20070318...
- deleteã¨destroyã®éã
æä½ã®å¯¾è±¡ã
- ãã¡ã¢ãªä¸ããï¼ï¼æ°¸ç¶åãä¼´ããªãä¸æçãªæä½ãï¼ï¼
- ãããã¯ãDBä¸ããï¼ï¼æ°¸ç¶åãä¼´ãæçµç¢ºå®çãªæä½ãï¼ï¼
ã¨ãã観ç¹ã§èããã¨ï¼ã©ããªã¢ãã«ã¡ã½ããã§ãã£ã¦ããã£ããåé¡ã§ããã
æ¬æ¥ï¼ãCRUDãã¯æ°¸ç¶åã¬ã¤ã¤ã«ç¨ããç¨èªã§ãã£ã¦ï¼ãã¡ã¢ãªä¸ã§CRUDããããªã©ã¨è¨ã£ããã¯ããªãã
ã ãï¼ä¾¿å®ä¸CRUDã«å¯¾å¿ä»ããã¨ï¼ãã¯ããã£ããåé¡ã§ããã
ãORM(object-Relational Mapping)ãã¬ã¼ã ã¯ã¼ã¯ã使ã£ã¦ããå ´åã¯ï¼ã¡ã¢ãªä¸ã§ã®ã¬ã³ã¼ããªãã¸ã§ã¯ãã®çææ¶æ» ãï¼ä¸ç¨®ã®CRUDã¨å¼ã¶äºã«ãããããã¨ããäºã§ããã
ä¾ãã°ï¼ãèªã¿åãããè¡ãªãã¡ã½ããã¯ï¼ä»¥ä¸ã®ï¼ç¨®é¡ããã¨è¨ããã
- ï¼å¦ççã£æä¸ã®ï¼APãµã¼ãã®ã¡ã¢ãªä¸ã«ï¼ãè«ççã«ãåå¨ããï¼ãªãã¸ã§ã¯ãã«å¯¾ãã¦ç¶æ
ãåãåãããã¡ã½ããã
- ç義ã®getterãï¼
- åºç¾©ã®getterï¼åã«ãªãã¸ã§ã¯ãã®ãã£ã¼ã«ããåå¾ããã ãã§ãªãï¼ä½ããã®æ¼ç®ãä¼´ã£ã¦ï¼ãã®ãªãã¸ã§ã¯ãã«é¢ããæç¨ãªæ å ±ãå¼ãåºãã¡ã½ããï¼ã
- ï¼ç¢ºå®æ¸ã¿ã®ï¼DBãµã¼ãã®ãã¡ã¤ã«ã·ã¹ãã ä¸ã«ãç©ççã«*1ãåå¨ããï¼DBã®ã¬ã³ã¼ãã«å¯¾ãã¦ç¶æ
ãåãåãããã¡ã½ããã
- ç´ç²ãªæå³ã§ã®ãåç §ç³»ã®ã¡ã½ãããã
- ãã¡ã¤ã³ãï¼æ¤ç´¢ã
â»ä¸ã®è¡¨ã«ã¯ï¼ãã®ï¼ã¤ã«å ãã¦reloadãè¨è¼ãã¦ããããã®ã¡ã½ããã¯æ°¸ç¶åã¬ã¤ã¤ã¸ã®ã¢ã¯ã»ã¹ãä¼´ãã
ï¼ï¼ï¼ã¡ã½ããã®æ¸ãåãè¦ç´
ãããï¼ã¢ãã«å
ã«è¨è¿°ããã¡ã½ããã®æ¸ãåãï¼æ£²ã¿åãï¼æ¹ã
- publicã¨privateã§ã¯ï¼
- publicãå ã«æ¸ããï¼å ¬éAPIãªã®ã§ï¼
- publicã®ã³ã¼ãéãå¯è½ãªéãæ¸ãããï¼ã¤ã³ã¿ãã§ã¼ã¹ã¨å®è£ ãåãé¢ãï¼
- ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã¨ã¯ã©ã¹ã¡ã½ããã§ã¯ï¼
- ã¯ã©ã¹ã¡ã½ãããå ã«æ¸ããï¼ä¸è¬çãªã³ã¼ãã£ã³ã°è¦ç´ï¼
- ã¯ã©ã¹ã¡ã½ãããé¿ãï¼ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãããå¯è½ãªéãå å®ããããï¼ãªãã¸ã§ã¯ãæåã¯ã¤ã³ã¹ã¿ã³ã¹ä¸å¿ã®ããã°ã©ãã³ã°ã¹ã¿ã¤ã«ï¼
- æä½å¯¾è±¡ã®ã¬ã³ã¼ãæ°ã«ã¤ãã¦
- åä¸ã¬ã³ã¼ããæ±ãå¦çãï¼åã¢ãã«å ã«è¨è¿°ããã
- è¤æ°ã¬ã³ã¼ã/è¤æ°ãã¼ãã«ãæ±ãå¦çãï¼ãã©ã³ã¶ã¯ã·ã§ã³ãå¿ è¦ã«ãªãå¦çã¯ï¼åã¢ãã«ã«è¨è¿°ããªãã親ã¢ãã«ã«è¨è¿°ããã
ã½ã¼ã¹ã³ã¼ãã§ã®ã¯ã©ã¹ã®è¨è¿°é åºï¼C++ï¼
http://d.hatena.ne.jp/c299792458/2010...
- publicãªAPIãå ã«æ¸ããããããã°å¤æ´ãããããè¦ç´ ãå¾ãã®ã»ãã«æ¼ããããã¨ãã§ããã
ã¡ã½ãã (è¨ç®æ©ç§å¦)
http://ja.wikipedia.org/wiki/%E3%83%A...
ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãã (instance method) ã¨ã¯ã¤ã³ã¹ã¿ã³ã¹ã«å±ããã¡ã½ããã®ãã¨ã§ãããçæãããã¤ã³ã¹ã¿ã³ã¹ããã¢ã¯ã»ã¹ãããã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã¯ãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã®ä¸æ ¸ããªãããã£ã¨ããã使ãããã
Rubyã³ã¼ãã£ã³ã°è¦ç´
http://shugo.net/ruby-codeconv/codeco...
ã¡ã½ããã®è¨è¿°é åº
Ruby on Railsã®ãããMVCãã®å¼å®³
http://satoshi.blogs.com/life/2009/10...
Railsã®ActiveRecordå©ç¨æã«ã¯ï¼DBã¢ã¯ã»ã¹å±¤ï¼åã¢ãã«ï¼ã®ä¸ã«ãã¸ãã¹ãã¸ãã¯å±¤ï¼è¦ªã¢ãã«ï¼ãè¨ãã
ãªã¹ãã«ããã¨ï¼ä¸è¨ã®ããã«ãªãã
- publicï¼æ¸ããï¼
- classã¡ã½ããï¼æ¸ããï¼
- DBæä½
- C
- Rï¼â»ããã ãã¯ã©ããã¦ãæ¤ç´¢ç³»ã®ã¡ã½ãããå¢ããï¼
- U
- D
- DBæä½
- instanceã¡ã½ããï¼å¢ããï¼
- ã¡ã¢ãªä¸æä½
- C
- Rï¼åºç¾©ã®ã¢ã¯ã»ãµï¼
- U
- D
- DBæä½
- C
- R
- U
- D
- ã¡ã¢ãªä¸æä½
- classã¡ã½ããï¼æ¸ããï¼
- privateï¼å¢ããï¼
- publicã¨åã
注ï¼
- privateãªã¯ã©ã¹ã¡ã½ããã¯ï¼ãprivate_class_method ã§å®ç¾©ã
- privateãªã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã¯ï¼ãprivate ã§å®ç¾©ã
Rubyã§ã¯ã©ã¹ã¡ã½ãããprivateã«ããæ£ããæ¹æ³
http://blog.s21g.com/articles/561
- ãã ãï¼ç¹ç°ã¡ã½ããã使ãæ¹ãä¸è¬ç
ã
*1:ç©ççã¨ã¯è¨ã£ã¦ãï¼æè¿ã¯ãªã³ã¡ã¢ãªDBãªã©ãããã®ã ãã»ã»ã»