rails-api 基本

官网:http://edgeguides.rubyonrails.org/api_app.html

创建:

  rails new my_api --api

改为mysql2:

  改动处01:

    Gemfile

      添加 gem 'mysql2'

  改动处02:(例子)

    config/database.yml

      default: &default
        adapter: mysql2
        pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
        timeout: 5000
        username: root
        password: wuhan85123_
        host: 111.231.xxx.107

      development:
        <<: *default
        database: xwhgame

模型:

  生成命令:

    rails generate model User name:string email:string

  撤销命令:

     rails destroy model User

  特殊情况: 

    比如有两个rails应用都在操作同一个数据库,一个正常生产模型(rails generate model Games_config ...)然后rails db:migrate,

      另一个需要用同样的生产模型语句(rails generate model Games_config ...),比如生成有下面信息:

[root@192 game_root_api]# rails generate model Games_config rooms:integer name:string lev:string lev_money:integer fk:boolean
      invoke  active_record
      create    db/migrate/20180719153904_create_games_configs.rb
      create    app/models/games_config.rb
      invoke    test_unit
      create      test/models/games_config_test.rb
      create      test/fixtures/games_configs.yml
[root@192 game_root_api]#

      但接着要这个应用删除掉生成的migrate文件,然后才可以用生成的model文件操作数据库,不然会报错

      这样看下来,手写model文件应该也可以,还没试

Active Record Basics:

  如读取数据:

    http://guides.rubyonrails.org/active_record_basics.html#read

  CURD可以仔细看看

migrate:http://guides.rubyonrails.org/active_record_migrations.html#changing-columns

  创建:

    rails generate model Bidaxiao_cofig rooms:integer

  执行:

    rails db:migrate

  额外提示:

    一些mysql2可填写的数据类型 string integer boolean datetime  date float

  例子:(注意命名规范)

    添加列:

      命令:rails generate migration AddPasToUsers

      生成的文件:

        [root@192 game_root_api]# more db/migrate/20180716180008_add_pas_to_users.rb
                     class AddPasToUsers < ActiveRecord::Migration[5.2]
                        def change
                               add_column :users, :pas, :string
                        end
                     end
        [root@192 game_root_api]#

    删除列:

      命令:rails generate migration RemovePasswordFromUsers

      生成的文件:

        [root@192 game_root_api]# more db/migrate/20180716175253_remove_password_from_users.rb
                     class RemovePasswordFromUsers < ActiveRecord::Migration[5.2]
                        def change
                               remove_column :users, :password, :string
                        end
                     end
        [root@192 game_root_api]#

    添加限制:

      命令:rails generate migration AddLimitToEnameOfUsers

      生成的文件:

        [root@192 game_root_api]# more db/migrate/20180803182811_add_limit_to_ename_of_users.rb
                     class AddLimitToEnameOfUsers < ActiveRecord::Migration[5.2]
                        def change
                              change_column :users, :ename, :string, :limit=> 20 (此行自己添加的)
                        end
                     end
        [root@192 game_root_api]#

      命令:rails generate migration SetEnameUniqueTrue

      生成的文件:

        [root@192 game_root_api]# more db/migrate/20180921023753_set_ename_unique_true.rb
                     class SetEnameUniqueTrue < ActiveRecord::Migration[5.2]
                        def change
                              add_index :users, [:ename], :unique => true
                        end
                     end
        [root@192 game_root_api]#

简单路由:

        [root@192 game_root_api]# more config/routes.rb
          Rails.application.routes.draw do
            resources :users
            # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

                  root 'application#hello'
                  post '/registry' => 'application#registry'
                  post '/login' => 'application#login'
                  post '/get_game_config' => 'application#get_game_config'
          end
        [root@192 game_root_api]#    

查看路由列表:

  命令:  

    rails routes

常用命令:

  启动:

    rails server

  安装指定版本rails

    gem install rails -v 5.1.4  

  数据库方面:

    迁移数据库:

      rails db:migrate

    撤销前一个迁移操作:

      rails db:rollback

    回到最开始的状态:

       rails db:migrate VERSION=0

    向数据库中随便添加些数据:

      rails db:seed

    暂时不知道什么用:(书上写的是"还原数据库")

       rails db:migrate:reset

  bundle install

  bundle update

  rails console --sandbox

  rails console

  

   

posted on   --LP--  阅读(663)  评论(0编辑  收藏  举报

(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示