ActiveRecord 㨠ActiveSupport ã使ã£ã¦ã³ãã³ãã©ã¤ã³ã¢ããªãä½ã
ç®ã®åã«ã¯csvã®å±±ãããããããrdbã«çªã£è¾¼ã¾ãªãã¨ãããªãããã®å¾railsã¢ããªããã®rdbã使ãã®ã§ãããcsvã¯ä½ç¨®é¡ããã£ã¦ï¼ç´ ç´ã«ãã¼ãã«ã«å¯¾å¿ãã¦ãããããããªããã¤ã¼ãã¨ã¯çªã£è¾¼ãã¨ãã«ActiveRecordã使ããã°ã©ã¯ãããã¨æã£ããARã ããããªãã¦ï¼ä¾¿å©ãªã¡ã½ãããããããå ¥ã£ã¦ããActiveSupportã使ããããã¨ããããä½ãèããã«ã¹ã¯ãªãããæ¸ãã¦ã¿ãã
require 'rubygems' require_gem 'activerecord'
ã¨ããã°ARã¯ï¼ActiveSupportãï¼ä½¿ããããã«ãªããã§ãã¢ãã«ã¯railsã§ä½ã£ãã¢ãã«ããã®ã¾ã¾ä½¿ããããåãå®ç¾©ãã³ãããã¦ããããã ãã©ï¼ãã¼ã¸ã§ã³ãããããããã¨å°ããã
require 'rubygems' require_gem 'activerecord' $LOAD_PATH.push('/path/to/models')
ãããã£ã¦ã¢ãã«ã®ãã£ã¬ã¯ããªã$LOAD_PATHã«å ¥ãã¦ããã ãã§ãããããã¯ActiveSupportã®æ©è½ã§ï¼$LOAD_PATHã«ããã¯ã©ã¹ã¯èªåçã«ä½¿ããããã«ãªãï¼ãã¡ã¤ã«å==ã¯ã©ã¹åã§ããå¿ è¦ãããï¼ãDBã«æ¥ç¶ããã¨ããã¯èªåã§ãããªãã¨ãããªããã©ï¼ARã«ã¡ã½ãããç¨æããã¦ããã®ã§ç°¡åã
require 'rubygems' require_gem 'activerecord' $LOAD_PATH.push('/path/to/models') ActiveRecord::Base.establish_connection( :adapter => 'hoge_db', :database => 'hoge_development', :username => 'hoge_user', :password => 'hoge_pw', :host =>'localhost' )
ããå¾ ã¦ãdatabase.ymlãè¦ã¦ã»ãããªããããç°¡åã
require 'rubygems' require_gem 'activerecord' $LOAD_PATH.push('/path/to/models') config = YAML.load_file('/path/to/config/database.yml') ActiveRecord::Base.establish_connection( configl['development'] )
ãããªæãã§ï¼ã³ãã³ãã©ã¤ã³ããARã使ãã¹ã¯ãªããã¯ç°¡åã«ã§ãããé¢åãªå¤é¨åç
§ã¨ãARã«ä»»ããããã®ã¯å¬ããã
ã§æ¬¡ã«acts_as_searchableã使ããã¨æã£ããå
¨ææ¤ç´¢ãå¿
è¦ã ããï¼rdbã«çªã£è¾¼ãã¨ãã«ã¤ã³ããã¯ã¹ãä½ã£ã¦ãã¾ãã°ããã ããã¨ããèããã¨ãããï¼acts_as_searchableãã¤ã³ã¹ãã¼ã«ãã¦ï¼ã¢ãã«ã«è¨è¿°ãã¦ããAR#saveããã¨ã¨ã©ã¼ã«ãªããacts_as_searchableã¯Hyper Estraierã¨ããã¨ãããã¨ãã®è¨å®ãdatabase.ymlã«è¨è¿°ããã®ã ãã©ï¼ãã®è¨å®ï¼ä¸è¨ã®configï¼ã渡ã£ã¦ããªããæ®æ®µã¯railsã渡ãã¦ãããã®ã ãã©ï¼ä»ã¯èªåã§è¨å®ãã¦ãããã渡ããªãããã«æãããããï¼æ¸¡ãæ¹ãåãããªãããã¾ããã§æ«æä¸ã
ãscript/consoleã使ããæ¡ãid:kdmsnrããã«æãã¦ããã£ããrailsã¢ããªã®scriptãã£ã¬ã¯ããªã®ä¸ã«consoleã¨ããirbãrailsåãã«æ¡å¼µãããã¼ã«ãå
¥ã£ã¦ãããããã¤ã¯èµ·åæã«railsç°å¢ãèªã¿è¾¼ãã®ã§ï¼ããã«ã¹ã¯ãªãããèªã¿è¾¼ã¾ããã°ãããåæåã¯å
¨é¨ãã£ã¦ãããã®ã§ï¼ä¸ã«æ¸ãããããªDBæ¥ç¶ãªã©ãä¸è¦ã«ãªãã
ruby script/console < hoge.rb
ãããªæããéã«cosoleã®åæåæã«èµ°ãã³ã¼ããhoge.rbã«ã³ãããã¦ãããã ããã
id:kakutaniããã«æãã¦ããã£ãã®ã¯script/runnerã£ã¦ãã¤ãããã¤ã¯å¼æ°ããããªãevalãã¡ããã®ã§ï¼ã¹ã¯ãªãããã¡ã¤ã«ãå®è¡ããããã¨æãã¨ã¡ãã£ã¨å¦ãªãã¨ãããªãã¨ãããªãããã®ãããscript/consoleã®ãããªä½è¨ãªæ©è½ããªãããããªæãã§ãã£ã¦ã¿ãããã£ã¨ããæ¹æ³ãããã®ã ãããã
ruby runner 'eval(IO.readlines("hoge.rb").join)'
ãä»åã®åèå³æ¸ã
Ruby on Railså ¥éåªããRailsã®è²ã¦æ¹
- ä½è : 西åå
- åºç社/ã¡ã¼ã«ã¼: ç§åã·ã¹ãã
- çºå£²æ¥: 2006/07/31
- ã¡ãã£ã¢: åè¡æ¬
- è³¼å ¥: 2人 ã¯ãªãã¯: 237å
- ãã®ååãå«ãããã° (118件) ãè¦ã