rails æ¢åDBã使ã£ã¦railsãã¦ã¿ãã
ç°å¢
- osx10.9.1
- ruby2.0.0
- rails4
- MySQL5.6.x
çµç·¯
æ¢ã«MySQLä¸ã«åå¨ããtableã使ã£ã¦railsãã¦ã¿ãã
MySQLã¨æ¥ç¶ãã
ä¸ã®ãµã¤ããåèã«ãã¦ãdatabase.yamlãå®ç¾©ã
- development: adapter: mysql2 encoding: utf8 database: my_db_name username: root password: root host: localhost pool: 5 timeout: 5000
åèãµã¤ã http://threetreeslight.com/post/61834000065/rails-db
MySQLä¸ã®ãã¼ãã«ããscheme.rbãçæããã
rake environment -v -t RAILS_ENV=development db:schema:dump
åèãµã¤ã http://threetreeslight.com/post/61834000065/rails-db
scheme.rbãåèã«ãã¦generate scaffoldãããgenerate model ãã
rails consoleä¸ã§ãã¼ãã«ã®å±æ§åã¨åã®ä¸è¦§ãåå¾ããã
(â»ã§ããã°rails consoleã§rbãã¡ã¤ã«ãç´æ¥å®è¡ãããããã)
rails console
table_name = 'master_company' #ããã«DBã®ãã¼ãã«å field_names = Array.new cols = ActiveRecord::Base.connection.columns(table_name) #*1 cols.each do |c| field_names << "#{c.name}:#{c.type}" end puts field_names.join(' ') # ãã®åºåçµæãã³ãããã¦ããã
rails g scaffold Company ã³ããããå 容ããã¼ã¹ã --skip-migration # *2
åèãµã¤ã http://kagen88.blogspot.jp/2012/12/rails.html
åèå ãµã¤ãããã®å¤æ´ç¹
- columnsã¡ã½ããã®å¼æ°ãï¼ã¤ããï¼ã¤ã«å¤æ´
- --skip-migrate ã --skip-migrationã«å¤æ´
modelã®ãã¼ãã«åãå¤æ´ããã
class Company < ActiveRecord::Base self.table_name='master_company' end
åèãµã¤ã http://threetreeslight.com/post/61834000065/rails-db http://kagen88.blogspot.jp/2012/12/rails.html
modelã®ä¸»ãã¼ãå¤æ´ããã
class Company < ActiveRecord::Base self.table_name='master_company' self.primary_key=:company_id end
åèãµã¤ã http://threetreeslight.com/post/61834000065/rails-db http://kagen88.blogspot.jp/2012/12/rails.html
modelã«è¤åãã¼ãè¨å®ããã
composite_primary_keysã©ã¤ãã©ãªãå©ç¨ããã
class CompanyEmploy < ActiveRecord::Base self.primary_keys=:company_id, :employ_id end
åèãµã¤ã http://www.oiax.jp/rails/zakkan/composite_primary_keys_1.html
æ®é ç®
- railsã®äºç´åã¨æ¢åDBã®åããã¶ã£ãå ´å
- ä½ææ¥ãæ´æ°æ¥ã®å¯¾å¿
- è«çåé¤å¯¾å¿