ruby on rails入门笔记(1)-数据库迁移

Ruby on Rails 快速入门 theme: condensed-night-purple ruby china wiki:https://ruby-china.org/wiki The Rails Doctrine: https://ruby-china.org/wiki/the-rails-doctrine Guides: https://guides.rubyonrails.org/ https://ru... 阅读详情

最近想学ruby on rails在网上也没找到什么入门的笔记教程,其它全是电子书的下载,现有的笔记也只是最基本的。

要不就是些配置教程。

在《practical rails social networking sites ruby on rals》书上看了些,觉得不错,写出来分享一下。

 

首先建立一个平台$ rails railscoders --database=mysql    #指定数据库为mysql

 

在数据库的导入之前先建立一个数据库

$ mysql -u root -p

password

>create database railscoders_development

此时测试一下数据库是否可以迁移

$ rake db:migrate

如果没有错误信息出现,恭喜你,迁移成功了!

 

解释一下:

(1)这里的数据库配置文件在

../railscoders/config/database.yml

在这个文件中你可能需要修改你数据库的密码!!!

(2)如果你想了解一下rake

你可以:

$ rake -T

你就可以看到你的rake命令可以做哪些事情了,哈哈

##########################

贴一下我的输出

(in /home/hugewave/myprograme/rails/first)
rake db:abort_if_pending_migrations       # Raises an error if there are pending migrations
rake db:charset                           # Retrieves the charset for the current environment's database
rake db:collation                         # Retrieves the collation for the current environment's database
rake db:create                            # Create the database defined in config/database.yml for the current RAILS_ENV
rake db:create:all                        # Create all the local databases defined in config/database.yml
rake db:drop                              # Drops the database for the current RAILS_ENV
rake db:drop:all                          # Drops all the local databases defined in config/database.yml
rake db:fixtures:identify                 # Search for a fixture given a LABEL or ID.
rake db:fixtures:load                     # Load fixtures into the current environment's database.
rake db:migrate                           # Migrate the database through scripts in db/migrate and update db/schema.rb by invoking db:schema:dump. Target specific version with VERSION=x. Turn off output with VERBOSE=false.
rake db:migrate:down                      # Runs the "down" for a given migration VERSION.
rake db:migrate:redo                      # Rollbacks the database one migration and re migrate up.
rake db:migrate:reset                     # Resets your database using your migrations for the current environment
rake db:migrate:up                        # Runs the "up" for a given migration VERSION.
rake db:reset                             # Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.
rake db:rollback                          # Rolls the schema back to the previous version.
rake db:schema:dump                       # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load                       # Load a schema.rb file into the database
rake db:seed                              # Load the seed data from db/seeds.rb
rake db:sessions:clear                    # Clear the sessions table
rake db:sessions:create                   # Creates a sessions migration for use with ActiveRecord::SessionStore
rake db:setup                             # Create the database, load the schema, and initialize with the seed data
rake db:structure:dump                    # Dump the database structure to a SQL file
rake db:test:clone                        # Recreate the test database from the current environment's database schema
rake db:test:clone_structure              # Recreate the test databases from the development structure
rake db:test:load                         # Recreate the test database from the current schema.rb
rake db:test:prepare                      # Check for pending migrations and load the test schema
rake db:test:purge                        # Empty the test database
rake db:version                           # Retrieves the current schema version number
rake doc:app                              # Build the RDOC HTML Files
rake doc:clobber_app                      # Remove rdoc products
rake doc:clobber_plugins                  # Remove plugin documentation
rake doc:clobber_rails                    # Remove rdoc products
rake doc:guides                           # Generate Rails guides
rake doc:plugins                          # Generate documentation for all installed plugins
rake doc:rails                            # Build the RDOC HTML Files
rake doc:reapp                            # Force a rebuild of the RDOC files
rake doc:rerails                          # Force a rebuild of the RDOC files
rake gems                                 # List the gems that this rails application depends on
rake gems:build                           # Build any native extensions for unpacked gems
rake gems:build:force                     # Force the build of all gems
rake gems:install                         # Installs all required gems.
rake gems:refresh_specs                   # Regenerate gem specifications in correct format.
rake gems:unpack                          # Unpacks all required gems into vendor/gems.
rake gems:unpack:dependencies             # Unpacks all required gems and their dependencies into vendor/gems.
rake log:clear                            # Truncates all *.log files in log/ to zero bytes
rake middleware                           # Prints out your Rack middleware stack
rake notes                                # Enumerate all annotations
rake notes:custom                         # Enumerate a custom annotation, specify with ANNOTATION=WTFHAX
rake notes:fixme                          # Enumerate all FIXME annotations
rake notes:optimize                       # Enumerate all OPTIMIZE annotations
rake notes:todo                           # Enumerate all TODO annotations
rake rails:freeze:edge                    # Lock to latest Edge Rails, for a specific release use RELEASE=1.2.0
rake rails:freeze:gems                    # Lock this application to the current gems (by unpacking them into vendor/rails)
rake rails:template                       # Applies the template supplied by LOCATION=/path/to/template
rake rails:unfreeze                       # Unlock this application from freeze of gems or edge and return to a fluid use of system gems
rake rails:update                         # Update both configs, scripts and public/javascripts from Rails
rake rails:update:application_controller  # Rename application.rb to application_controller.rb
rake rails:update:configs                 # Update config/boot.rb from your current rails install
rake rails:update:generate_dispatchers    # Generate dispatcher files in RAILS_ROOT/public
rake rails:update:javascripts             # Update your javascripts from your current rails install
rake rails:update:scripts                 # Add new scripts to the application script/ directory
rake routes                               # Print out all defined routes in match order, with names.
rake secret                               # Generate a crytographically secure secret key.
rake stats                                # Report code statistics (KLOCs, etc) from the application
rake test                                 # Run all unit, functional and integration tests
rake test:benchmark                       # Run tests for benchmarkdb:test:prepare / Benchmark the performance tests
rake test:functionals                     # Run tests for functionalsdb:test:prepare / Run the functional tests in test/functional
rake test:integration                     # Run tests for integrationdb:test:prepare / Run the integration tests in test/integration
rake test:plugins                         # Run tests for pluginsenvironment / Run the plugin tests in vendor/plugins/*/**/test (or specify with PLUGIN=name)
rake test:profile                         # Run tests for profiledb:test:prepare / Profile the performance tests
rake test:recent                          # Run tests for recentdb:test:prepare / Test recent changes
rake test:uncommitted                     # Run tests for uncommitteddb:test:prepare / Test changes since last checkin (only Subversion and Git)
rake test:units                           # Run tests for unitsdb:test:prepare / Run the unit tests in test/unit
rake time:zones:all                       # Displays names of all time zones recognized by the Rails TimeZone class, grouped by offset.
rake time:zones:local                     # Displays names of time zones recognized by the Rails TimeZone class with the same offset as the system local time
rake time:zones:us                        # Displays names of US time zones recognized by the Rails TimeZone class, grouped by offset.
rake tmp:cache:clear                      # Clears all files and directories in tmp/cache
rake tmp:clear                            # Clear session, cache, and socket files from tmp/
rake tmp:create                           # Creates tmp directories for sessions, cache, sockets, and pids
rake tmp:pids:clear                       # Clears all files in tmp/pids
rake tmp:sessions:clear                   # Clears all files in tmp/sessions
rake tmp:sockets:clear                    # Clears all files in tmp/sockets

##########################

 

这里我们先跳过页面布局和CSS配置,这又是一大段的内容,而且这个我还很不了解,嘿嘿

 

建立一个Page模型来让我导入我们的数据库内容

$ ruby script/generate model Page

 

生成了模型,接着让我们来改一下我们的配置文件吧

../railscoder/db/migrate/20100326071901_create_pages.rb

注释一下,你的文件名可能不是这个,不过路径是这个,哈哈,好像目前也就只有这个文件

 

进去看一下,你会看见两个定义

def self.up

def self.down

 

把这个文件的内容改成这样:

class CreatePages < ActiveRecord::Migration
  def self.up
    create_table :pages do |t|
      t.column :title, :string
      t.column :permalink, :string
      t.column :body, :text
      t.column :created_at, :datetime
      t.column :updated_at, :datetime
    end
  end
  def self.down
    drop_table :pages
  end
end

 

可能你看这个还不太明白,那再改一下,把self.up方法改成这样:

  def self.up

    create_table :pages do |t|
      t.column :title, :string
      t.column :permalink, :string
      t.column :body, :text
      t.column :created_at, :datetime
      t.column :updated_at, :datetime
    end
    Page.create(:title => "RailsCoders Home",
        :permalink => "welcome-page",
        :body => "Welcome to RailsCoders")
  end

 

这样一来你大概已经明白差不多了吧。

就是建立一张表,包括什么内容呢?

就是包括了,title,permalink,body,create_at,update_at

其实如果你进数据库,类似的操作就是:

>create table pages(

>title varchar(200),

>permalink varchar(200),

>body varchar(200),

>create_at time,

>update_at time);

这样你就该明白了吧,哈哈,原来这么简单,用ruby导入数据就变得超级简单了

不过不要高兴的太早,你还没导进去呢!怎么进去,写个命令:

$ rake db:migrate

输出信息,会告诉你成功了的,哈哈

 

如果你不信你可以进去看一下

mysql> use railscoders_development;

 

mysql> select * from pages;

你是不是已经看到那个文件里面导入的信息了,哈哈!

 

好了,数据的迁移这样就结束了

 

 

 

##########################

最后贴一下我的ruby on rals 配置

Ruby version1.8.7 (i486-linux)
RubyGems version1.3.5
Rack version1.0
Rails version2.3.5
Active Record version2.3.5
Active Resource version2.3.5
Action Mailer version2.3.5
Active Support version2.3.5
Application root/home/hugewave/myprograme/rails/first
Environmentdevelopment
Database adaptersqlite3
Database schema version0

##########################

Ruby On Rails 笔记3——表的增删改查 Rails 使用此时间戳来决定应运行哪个migration,以及运行的顺序,因此,如果要从其他应用程序复制migration或自己生成文件,请注意其在顺序中的位置,经常发生在合并代码的时候。A schema开始时什么都没有,每次migration都会对其进行修改,以添加或删除表、列或索引。如果你本地数据库和线上数据库不是完全一致,你就会发现运行完migration后schema.rb文件中也会用大量改动,这个时候你需要手动进去修改你需要的改动,把其他的改动恢复原样,然后才能提交。 阅读详情

相关推荐

Ruby on Rails开发实践与技巧

本文详细讲解了Ruby on Rails开发中的多个实用技巧,包括数据库表扩展、视图条件渲染、字符串方法调用、异常处理机制、自定义类方法、模型比较与模块混入、迁移与模型命名规范、数据库操作、自定义查找器、Rails视图优化、数据验证、日志优化、异常通知插件配置、多实例部署及跨平台部署等内容,适用于希望提升Rails开发能力的开发者。

Pepper的专栏 962

Ruby on Rails 教程之快速入门

一,简介 Ruby on Rails是一个框架,一个使Web 应用的开发、部署和维护变得更容易的框架。自从发布第一个版本以来, Rails 已经从一个默默无闻的玩具蜕变成了一种世界性的现象。它拿下了一个又一个的奖项,更重要的是,很多web2.0 应用都选择它作为基础框架。Rails 已经不再只是一小撮黑客推崇的时髦玩艺:很多跨国公司正在用它来开发自己的web 应用。 为什么会这样? 有几个方...

不坠青云 1万+

CentOS7下通过YUM源高效部署Zabbix7 Agent指南

本文详细指导在已结束生命周期的CentOS7系统上,通过更换YUM源(如阿里云镜像)并添加Zabbix官方仓库,高效部署Zabbix7 Agent。文章涵盖从源配置、软件安装、关键参数修改到服务启动与防火墙设置的全流程,帮助用户解决依赖错误,快速建立服务器监控。

weixin_29230059的博客 270

Ruby on Rails,数据库迁移命令和迁移任务编写

在《Ruby on Rails,rake工具使用和数据库migrations迁移的概念》和《Ruby on Rails,创建和执行migrations迁移文件》中对Rails中数据库迁移的概念和迁移任务的执行做了了解。其中的迁移任务仅仅停留在创建和删除表之类最简单的操作,实际开发中还有很多对数据库结构和内容的调整。下面来了解一下: 创建表 [ruby] view pla

maxiaokun55的专栏 961

【阿里云大模型高级工程师ACP习题集】2.5 优化RAG应用提升问答准确度(⭐️⭐️⭐️ 重点章节!!!)

【摘要】这是我精心整理出对应的习题集,里面足足有 30 道题目,从基础概念到深度拓展,全方位覆盖章节重点、难点。不管你是想检验自己的学习成果,还是想针对薄弱环节进行强化训练,它都能完美适配!

davidkorhenn的博客 1122

ArcGIS图解建模工具与DEM提取山脊、山谷线(水文分析、表面分析)

参考汤国安、杨昕等编著的《地理信息系统空间分析实验教程第二版》 利用水文分析(流量)和表面分析(坡向变率)得到山脊和山谷线 文章目录1、基本概念1.1 正地形1.2 负地形正负地形的影响2、水文分析方法2.1 基本思想原理2.2 技术路线2.3 操作步骤2.3.1 正负地形的提取2.3.2 利用水文分析思想和工具提取研究区域的山脊线2.3.3 利用水文分析思想和工具提取研究区域的山谷线2.3.4 利用水文分析提取山脊线和山谷线结果3、表面分析方法3.1 基本思想原理3.2 技术路线3.3 操作步骤4、图.

fungis 1万+

Ruby on rails 实战圣经:数据库迁移 - Migrations

Programmingtoday is a race between software engineers striving to build bigger and betteridiot-proof programs, and the Universe trying to produce bigger and betteridiots. So far, the Universe is win

Felomeng的技术博客 2万+

OpenClaw 接入智谱 GLM-5-Turbo 龙虾套餐:完整避坑指南与 SOP

OpenClaw 接入智谱 GLM-5-Turbo 的核心在于“端点匹配”和“多渠道验证”。端点匹配:龙虾套餐必须用标准端点。多渠道验证:遇到 TUI 不显示时,用curl或飞书机器人验证,不要被 Bug 误导。希望这份 SOP 能帮你顺利跑通 OpenClaw + GLM-5-Turbo,享受强大的 AI 编程助手!

Aaron_liu20的博客 1723

Ruby on Rails 入门学习

Rails是什么? 一个快速搭建网页的框架,包含前后端和数据库,设计的思想是MVC。 可以快速搭建起网页。 Getting Started with Rails — Ruby on Rails Guides

DedicateToAI的博客 1021

使用KITTI数据集测试不同算法的性能

该文章将使用同一个kitti数据集来测试不同算法的性能,目前已经测试的算法包括:LIO-SAM,FAST-LIO2,Point-LIO,Faster-LIO。

wwtx的博客 2597

华为OD机考双机位C卷 - 员工派遣 (Java & Python& JS & C/C++ & GO )

华为OD机考双机位C卷 - 员工派遣 (OD上机考试双机位C卷)。提供在线OJ刷题。Java、C++、Python、GO语言、C语言、JavaScript题解,并提供完整思路讲解。

算法大师! 1046

ruby on rails_DB2和Ruby on Rails入门

Ruby on Rails于2004年发布,已Swift成为Web应用程序开发中最流行的框架之一。 这个开源项目也称为Rails或RoR,它使用模型-视图-控制器(MVC)架构,Ruby面向对象的脚本语言以及简单的原则,例如“约定之上的配置”和“不要重复自己”。 因此,开发人员可以通过更少的冗余代码和配置文件,更快,更轻松地构建应用程序而受益,同时保留创建自定义扩展的灵活性,以满足他们的应用程...

cuxiong8996的博客 668

YOLO126优化:损失篇 | AAAI 2025 | 一种基于尺度的动态(SD)损失,根据目标大小动态调整尺度和位置损失的影响,提高了网络检测不同尺度目标的能力

提出了一种基于尺度的动态(SD)损失,它根据目标大小动态调整尺度和位置损失的影响,提高了网络检测不同尺度目标的能力。

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 453

Ruby on Rails,创建和执行migrations迁移文件

欢迎来我的博客http://blog.csdn.net/abbuggy/article/details/7654481 在《Ruby on Rails,rake工具使用和数据库migrations迁移的概念》中,我们知道Rails中进行数据库迁移操作的基本概念和重要性。现在着手进行一个简单的数据库迁移实践吧。 所有的数据库迁移文件存放在simple_cms/db/migrati

边晓宇@CSDN 1万+

在 Android Studio 中接入 DeepSeek:加速 Android 开发进程

通过在 Android Studio 中接入 DeepSeek,Android 开发者能够充分利用人工智能技术带来的强大优势,显著提升开发效率和代码质量。从前期的安装准备,到具体的接入步骤,再到使用过程中的技巧和注意事项,每一个环节都为打造高效、智能的开发环境提供了有力支持。希望本文能帮助你顺利将 DeepSeek 融入到 Android Studio 的开发工作流程中,让你在 Android 开发的道路上更加得心应手,创造出更优质的 Android 应用。

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 4183

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Ruby on Rails部署简化:piku实现数据库迁移自动化

你是否还在为Ruby on Rails应用的部署流程感到头疼?从代码提交到数据库迁移,每个步骤都可能出错,尤其是数据库变更往往需要手动介入,既耗时又容易引发生产环境风险。本文将展示如何利用轻量级PaaS工具piku,通过简单配置实现Rails应用的全自动部署,重点解决数据库迁移的自动化难题。读完本文,你将掌握: - piku的核心部署流程与Ruby生态支持 - 如何通过Procfile定义数据库...

gitblog_01131的博客 1084

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

RailsCasts从入门到精通:Ruby on Rails开发者的实战学习路径

RailsCasts是一个开源的Ruby on Rails学习资源项目,提供免费的Ruby on Rails视频教程,帮助开发者快速掌握Rails框架的实战技能。通过这个项目,新手和普通用户可以系统地学习Rails开发,从基础到进阶,构建自己的Web应用。 ## 什么是RailsCasts? RailsCasts是一个专注于Ruby on Rails技术的视频教程网站,由Ryan Bates创

gitblog_01188的博客 657

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

【Ruby】Web框架ruby on rails初识(MVC架构初理解)

利用脚手架快速搭建一个有着CRUD功能的网站 我们只需要两条命令,就可以搭建出一个简单的学生管理系统的网站

Janium的博客 2330

系列总结:为什么Cosmos3-Nano-Sim-Auto是Physical AI仿真赛道的关键一步

Cosmos3-Nano-Sim-Auto 是 NVIDIA Cosmos3 世界模型家族中面向 Physical AI 赛道的**实时闭环驾驶仿真模型**:它可基于文本提示与 World Scenario Map(WSM)场景控制,联合生成最多 11 路同步的多视角相机视频与 LiDAR 点云,支撑自动驾驶策略的实时闭环评估与多传感器合成数据生成。本篇作为系列总结,带你理清它的系列定位、三大核心

gitblog_00659的博客 875

2024 最新 Ruby on Rails 入门指南:从安装到实战的完整教程

Ruby on Rails(简称 Rails)是一个使用 Ruby 语言编写的开源 Web 应用框架,遵循 MVC 架构模式,强调简洁、高效和约定优于配置的原则,被广泛应用于 Web 开发领域。本教程将带你快速掌握 Rails 的核心概念和使用方法,从零开始构建你的第一个 Rails 应用。 ## 为什么选择 Ruby on Rails? Rails 自 2004 年发布以来,凭借其"约定优于

gitblog_00789的博客 1127

Ruby on Rails 全栈开发:从数据库设计到前端页面搭建

在 Rails 中,通过路由来映射用户请求的 URL 与控制器的方法。打开config/routes.rb文件,定义文章相关的路由:endresources :articles会自动生成一系列 RESTful 风格的路由,对应文章控制器的各个方法。root 'articles#index'将文章列表页面设置为应用的首页。通过本教程,我们详细介绍了使用 Ruby on Rails 进行全栈开发的全过程,从开发环境搭建、数据库设计,到后端开发、前端页面搭建,再到项目的测试与部署。

数字魔方操控师的博客 995

清除或重新创建Ruby on Rails数据库

我有一个充满数据的开发Ruby on Rails数据库。 我想删除所有内容并重建数据库。 我正在考虑使用类似的东西: rake db:recreate 这可能吗?

p15097962069的博客 882

VideoCaptioner 长音频分块转录实战指南:ChunkedASR 与 ChunkMerger 原理、参数与源码级解析

> 本文围绕 VideoCaptioner(卡卡字幕助手)ASR 模块中的 `ChunkedASR` 装饰器与 `ChunkMerger` 合并器展开,讲解如何为任意 `BaseASR` 实现接入"分块-并发转录-智能合并"能力,以应对超过 20 分钟的长音频、避免公益 API 超时与内存溢出。读完本文,你将掌握分块参数的选型策略、`transcribe()` 工厂的自动接入方式,以及合并算法的精

gitblog_00078的博客 786

DB2 与 Ruby on Rails 入门

Ruby 语言的出现并与强大的 Rails 框架结合,为 Web 解决方案的开发带来了巨大的机遇。随着 IBM_DB 适配器和驱动程序的引入,Rails 应用程序现在可以无缝地与 IBM 数据服务器进行交互。本文是 DB2 wi...

ciyi7513的博客 270
上一篇: 24LC04读写,74HC595显示控制测试
下一篇: Flash Download failed-ARM7TDMI
conquerwave
博客等级 码龄18年 57粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值