Rails3 runnerãå©ç¨ãã¦ã§ãããããã°ã©ã ãå®è¡ãããµã³ãã«
Rails3ã§ãããããã°ã©ã ãå®è¡ããæ¹æ³ã¯å
¬å¼ã¬ã¤ãã§ã¯ä»¥ä¸ã®ããã«è¨è¼ããã¦ãã¾ã
http://guides.rubyonrails.org/command_line.html#rails-runner
$ rails runner "Model.long_running_method"
runnerã®ä½¿ãæ¹ã«ä»ãã¦ããã£ã¦ã¿ã¾ãããRailsã®æ§æã¾ã§å«ãããã説æãã¼ã¸ãç¡ãã£ãã®ã§ãã¾ã¨ãã¦ã¿ã¾ããã
ãµã³ãã«ã«ã¤ãã¦
ä»åã®ãµã³ãã«ã§ã¯ãèªè¨¼ã«deviseãå©ç¨ããã¢ããªã±ã¼ã·ã§ã³ã§ãã¦ã¼ã¶ã®æå¾
ãä¸åº¦ã«è¡ããããããã°ã©ã ãä½æãã¾ããã
ã¦ã¼ã¶ä¸è¦§ã¯CSVãã¡ã¤ã«ããèªã¿è¾¼ã¿ã¾ãã
deviseã®åºæ¬çãªæ©è½ã¨æå¾ ã®ä»çµã¿ã®è¿½å ã«ä»ãã¦ã¯ä»¥ä¸ã®è¨äºãåèã«ãã¦ãã ããã
ããã°ã©ã ã®ç½®ãå ´æã®æºå
ä»åãããããã°ã©ã ã¯lib/tasksã¨ãããã£ã¬ã¯ããªã«é
ç½®ãããã¨æãã¾ãã
lib以ä¸ã®ãã¡ã¤ã«ãèªã¿è¾¼ãããã«config/application.rbã«ä»¥ä¸ã追å ãã¾ãã
config.autoload_paths += %W(#{config.root}/lib) config.autoload_paths += Dir["#{config.root}/lib/**/"]
åè:http://hemju.com/2010/09/22/rails-3-quicktip-autoload-lib-directory-including-all-subdirectories/
lib/tasksãã£ã¬ã¯ããªã«invitation_task.rbãä½æãã¦ã以ä¸ã®ããã«ã¯ã©ã¹ãå®ç¾©ãã¾ããã
class Tasks::InvitationTask end
ã¡ã½ããã®å®è£
以ä¸ã¯ãã¡ã¼ã«ã¢ãã¬ã¹ãCSVããèªã¿åºããdevise inviteãå©ç¨ãã¦æå¾
ã¡ã¼ã«ãéä¿¡ãããããããã°ã©ã ã§ãã
å¼æ°ã«ãã¡ã¤ã«åãã¨ãã¾ãã
require "csv" class Tasks::InvitationPrivateBetaTask def self.execute(csv_file_name) CSV.foreach(csv_file_name) do |row| address = row[0] User.invite!(:email => address) end end end