javascript

java

python

c#

android

c++

node.js

php

html

jquery

ios

reactjs

css

.net

git

ruby-on-rails

sql

c

ruby

string

ruby-添加和删除参考的Rails迁移

使用rails generate migration AddClientToUser创建迁移文件后,我可以像这样编辑迁移文件:

class AddClientToUser < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.references :client
    end
  end

  def self.down
    change_table :users do |t|
      t.remove :client_id
    end
  end
end

这是反转迁移中添加的参考列的正确方法吗?

trans by 2020-08-12T09:15:54Z

Rails 4:通过迁移从表列中删除非null约束吗?

鉴于以下name

  create_table "people", force: true do |t|
    t.string   "name",  null: false
    t.integer  "age"
    t.integer  "height"
    t.string   "email"
    t.boolean  "married",  default: false
    t.text     "bio"
    t.integer  "fav_number"
    t.decimal  "lucky_num",  precision: 2, scale: 2
    t.datetime "birthday"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

我想删除name的默认值null: false。我尝试使用change_column_default运行单独的迁移,但是这对schema.rb没有影响。有什么建议吗?

trans by 2020-08-09T07:09:05Z

ruby-如何在Rails迁移中检查数据库类型?

我进行了以下迁移,并且希望能够检查与环境相关的当前数据库是否为mysql数据库。 如果是mysql,则要执行特定于数据库的SQL。

我该怎么办?

class AddUsersFb < ActiveRecord::Migration

  def self.up
    add_column :users, :fb_user_id, :integer
    add_column :users, :email_hash, :string
    #if mysql
    #execute("alter table users modify fb_user_id bigint")
  end

  def self.down
    remove_column :users, :fb_user_id
    remove_column :users, :email_hash
  end

end
trans by 2020-08-07T09:49:16Z

ruby on rails-Gem :: LoadError:无法激活pg(〜> 0.18),已经激活了pg-1.0.0

这个问题已经在这里有了答案:

  • 使用Postgres适配器的Rails应用程序无法激活pg 1个答案

我一直在做此处找到的Rails教程,并且成功完成了必须使用$ rails db:migrate迁移Comments迁移的过程。 在此之前,我已经能够生成Article模型并进行迁移,从而可以毫无问题地进行迁移。 在这两次迁移之间,我的Gemfile中没有任何变化,因此我不确定Bundler遇到了什么问题。

这是错误,然后是完整的命令行输出,以及我的Gemfile和schema.rb:

Gem::LoadError: can't activate pg (~> 0.18), already activated pg-1.0.0.

Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).

完整的命令行输出

xxx:gangelo: ~/dev/rails/test/blog (master*) ☠  rbenv exec rails db:migrate
rails aborted!
Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/connection_adapters/connection_specification.rb:188:in `rescue in spec'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/connection_adapters/connection_specification.rb:185:in `spec'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:880:in `establish_connection'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/connection_handling.rb:58:in `establish_connection'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/railtie.rb:124:in `block (2 levels) in <class:Railtie>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:69:in `instance_eval'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:69:in `block in execute_hook'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:60:in `with_execution_control'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:65:in `execute_hook'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:50:in `block in run_load_hooks'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:49:in `each'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:49:in `run_load_hooks'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/base.rb:326:in `<module:ActiveRecord>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/base.rb:25:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/mysql_database_tasks.rb:6:in `<class:MySQLDatabaseTasks>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/mysql_database_tasks.rb:3:in `<module:Tasks>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/mysql_database_tasks.rb:2:in `<module:ActiveRecord>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/mysql_database_tasks.rb:1:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:74:in `<module:DatabaseTasks>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:35:in `<module:Tasks>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:2:in `<module:ActiveRecord>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:1:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/railtie.rb:34:in `block (3 levels) in <class:Railtie>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/railties-5.1.4/lib/rails/commands/rake/rake_command.rb:21:in `block in perform'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/railties-5.1.4/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/railties-5.1.4/lib/rails/command.rb:46:in `invoke'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/bin/rails:9:in `require'
/Users/gangelo/dev/rails/test/blog/bin/rails:9:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
Gem::LoadError: can't activate pg (~> 0.18), already activated pg-1.0.0. Make sure all dependencies are added to Gemfile.
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:2:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/connection_adapters/connection_specification.rb:186:in `spec'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:880:in `establish_connection'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/connection_handling.rb:58:in `establish_connection'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/railtie.rb:124:in `block (2 levels) in <class:Railtie>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:69:in `instance_eval'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:69:in `block in execute_hook'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:60:in `with_execution_control'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:65:in `execute_hook'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:50:in `block in run_load_hooks'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:49:in `each'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:49:in `run_load_hooks'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/base.rb:326:in `<module:ActiveRecord>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/base.rb:25:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/mysql_database_tasks.rb:6:in `<class:MySQLDatabaseTasks>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/mysql_database_tasks.rb:3:in `<module:Tasks>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/mysql_database_tasks.rb:2:in `<module:ActiveRecord>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/mysql_database_tasks.rb:1:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:74:in `<module:DatabaseTasks>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:35:in `<module:Tasks>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:2:in `<module:ActiveRecord>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:1:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/activerecord-5.1.4/lib/active_record/railtie.rb:34:in `block (3 levels) in <class:Railtie>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/railties-5.1.4/lib/rails/commands/rake/rake_command.rb:21:in `block in perform'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/railties-5.1.4/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/railties-5.1.4/lib/rails/command.rb:46:in `invoke'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/bin/rails:9:in `require'
/Users/gangelo/dev/rails/test/blog/bin/rails:9:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/Users/gangelo/dev/rails/test/blog/vendor/bundle/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
/Users/gangelo/dev/rails/test/blog/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate => db:load_config
(See full trace by running task with --trace)

宝石文件

source 'https://rubygems.org'

ruby '2.3.1'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.4'

# Use sqlite3 as the database for Active Record
# gem 'sqlite3'

# Use postgres as the database for Active Record
gem 'pg'

# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
end

# gma - start
group :development, :test do
  gem 'rspec-rails', '~> 3.5', '>= 3.5.2'
  gem 'rspec-activemodel-mocks', '~> 1.0', '>= 1.0.3'
  gem 'shoulda-matchers', '~> 3.1', '>= 3.1.1'
  gem 'factory_bot_rails', '~> 4.8', '>= 4.8.2'
  gem 'ffaker', '~> 2.2'
  # gem 'timecop', '~> 0.8.1'
end
# gma - end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

模式.rb

ActiveRecord::Schema.define(version: 20180110153949) do
  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "articles", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
end

迁移档案

class CreateComments < ActiveRecord::Migration[5.1]
  def change
    create_table :comments do |t|
      t.string :commenter
      t.text :body
      t.references :article, foreign_key: true

      t.timestamps
    end
  end
end
trans by 2020-07-22T09:00:08Z

ruby on rails-如何使remove_column可逆?

我有一个删除列的迁移:

def change
  remove_column :foos, :bar, :boolean
end

当我尝试进行up的迁移时,出现以下错误:

remove_column is only reversible if given a type.

ActiveRecord :: Migration文档说以下是up的签名:

remove_column(table_name, column_name, type, options)

因此,在这种情况下,我的类型应该是up,我希望迁移是可逆的。 我想念什么?

我当然可以将其分解为updown迁移以避免此问题,但是我想了解为什么change语法在这种情况下不起作用。

trans by 2020-07-13T00:06:00Z

ruby-在Rails迁移中,如何删除字段的限制

以下正确吗?

 change_column :tablename, :fieldname, :limit => null
trans by 2020-02-16T02:39:17Z

将通过Rails迁移删除列,删除与该列关联的索引

在Rails 2中,通过Rails迁移删除列还会更改/删除与该列关联的索引吗? 如果不是,您还必须手动更改/删除每个索引,它是否应该自动化?

谢谢(来自Rails新手)

trans by 2020-02-13T03:37:25Z

Ruby on rails-运行rake:db migration命令时,出现错误“未初始化的常量CreateArticles”

我创建了一个模型红宝石脚本/生成模型文章(简单的内容)

这是迁移文件create_articles.rb:

def self.up
  create_table :articles do |t|
    t.column :user_id, :integer
    t.column :title, :string
    t.column :synopsis, :text, :limit => 1000
    t.column :body, :text, :limit => 20000
    t.column :published, :boolean, :default => false
    t.column :created_at, :datetime
    t.column :updated_at, :datetime
    t.column :published_at, :datetime
    t.column :category_id, :integer
  end

def self.down
  drop_table :articles
 end
end

当我运行rake:db migration命令时,我收到一个错误的rake中止了! “未初始化的常量CreateArticles。”

有谁知道为什么这个错误不断发生?

trans by 2020-02-08T20:23:38Z

连接选项卡的Rails命名约定

这个问题源于:在创建rails join table之后如何链接表单

我正在创建我的产品模型和类别模型之间的联接表。

联接表应命名为什么? Categories_products或category_products还是其他?

trans by 2020-01-29T09:57:07Z

heroku-在Rails 3中删除表和删除模型的最佳方法是什么?

我有一个不再需要在我的App中使用的模型和表格,我可以将它们放在此处,但我想删除它们以保持整洁。

我正在尝试找出删除它们的最佳方法,而不会弄乱我的迁移文件和db / schema.rb文件及其对我的生产环境可能产生的任何副作用,我的应用程序在Heroku上。 我在本地计算机和heroku上都使用PostgreSQL。

到目前为止,我已经找到了两种方法来执行此操作,但不确定哪种方法是最佳方法。

方法一

我考虑过只进入数据库并删除表,然后销毁模型。

rails db
DROP TABLE table_name
\q
rails destroy model model_name

如果执行此操作,此模型/表的迁移将发生什么? 我对此模型进行了两次迁移,分别是timestamp_create_modelname和add_attribute_to_table名称。

此方法还会更新db / schema.rb文件吗?

当我将应用程序推送到Heroku时,我怀疑模型会被删除,但表格会保留在原位,是否有heroku命令可以放下表格。

方法二

我读过的另一种方法是生成一个新的迁移,以删除表然后销毁模型。

rails generate migration drop_tablename

&然后更新以下文件:

db / migrate / timestamp drop_table名称(已更新,以回应下面的Dan回答)

class DropTablename < ActiveRecord::Migration
  def up
    drop_table :tablename
  end

  def down
    create_table :tablename do |t|
      t.string :table_column
      t.references :anothertable

      t.timestamps        
    end
    add_index :tablenames, :anothertable_id
  end
end

&然后在终端中:

rake db:migrate
rails destroy model model_name
rake db:migrate
git add .
git commit -m "removed table/model_name"
git push heroku master
heroku run rake db:migrate
heroku restart

这似乎是最好的方法,但是较旧的迁移文件会怎样? 每次我运行rake db:migrate时,它们是否仍会保留并更新db / shema,仅被db / migrate / timestamp_drop_tablename覆盖?

我很乐于尝试第二种方法,但是希望有经验的人参与其中,并告诉我这样做的方法。

trans by 2020-01-15T11:52:29Z

Rails迁移:表的add_reference但外键的列名与Rails约定不同

我有以下两个模型:

class Store < ActiveRecord::Base
    belongs_to :person
end

class Person < ActiveRecord::Base
    has_one :store
end

这是问题所在:我正在尝试创建一个迁移,以在人员表中创建外键。 但是,引用Store外键的列并未像rails约定那样被命名为store_id,而是被命名为foo_bar_store_id。

如果我遵循Rails约定,我将进行如下迁移:

class AddReferencesToPeople < ActiveRecord::Migration
  def change
    add_reference :people, :store, index: true
  end
end

但是,这将不起作用,因为列名不是store_id,而是foo_bar_store_id。 因此,如何指定外键名称只是不同,但仍保持index:true以保持快速性能?

trans by 2020-01-09T01:32:44Z

我为什么要运行'rake db:migrate RAILS_ENV = test'?

在运行迁移后,在Rails 4.0.0.rc1,Ruby 2.0.0上,尝试通过rake db:migrate RAILS_ENV=test运行测试时,看到以下错误:

/Users/peeja/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.rc1/lib/active_record/migration.rb:376:in   `check_pending!':迁移正在等待; 运行'rake db:migrate   RAILS_ENV = test'解决此问题。   (ActiveRecord :: PendingMigrationError)

那似乎不对。 没有人迁移他们的测试数据库,对吗? 他们是rake db:migrate RAILS_ENV=test,说实话,我已经忘记了。 因此,我运行rake db:test:prepare并再次运行rspec命令……并看到相同的错误。

如果我实际上是rake db:migrate RAILS_ENV=test,该错误实际上会消失。

这是怎么回事? 这是Rails 4中的新功能吗?

trans by 2019-11-08T10:37:20Z

rails中的t.belongs_to和t.references有什么区别?

t.referencest.belongs_to之间的差异有什么不同?为什么我们有这两个不同的词? 在我看来他们做同样的事情?试过一些谷歌搜索,但没有找到解释。

class CreateFoos < ActiveRecord::Migration
  def change
    create_table :foos do |t|
      t.references :bar
      t.belongs_to :baz
      # The two above seems to give similar results
      t.belongs_to :fooable, :polymorphic => true
      # I have not tried polymorphic with t.references
      t.timestamps
    end
  end
end
trans by 2019-07-24T19:25:56Z

ruby on rails - 如何生成迁移以使引用具有多态性

我有一个Products表,想要添加一列:

t.references :imageable, :polymorphic => true

我试图通过以下方式为此生成迁移:

$ rails generate migration AddImageableToProducts imageable:references:polymorphic

但我显然做错了。 任何人都可以提出任何建议吗? 谢谢

当我在生成迁移后尝试手动将其放入时,我这样做:

class AddImageableToProducts < ActiveRecord::Migration
  def self.up
    add_column :products, :imageable, :references, :polymorphic => true
  end

  def self.down
    remove_column :products, :imageable
  end
end

它仍然没有奏效

trans by 2019-07-10T10:41:46Z

ruby on rails - 在“引用”迁移中指定列名

我想在Rails中制作user_id,引用另一个表。 通常,我会这样做:

add_column :post, :user, :references

这将在posts表中创建名为user_id的列。 但是,如果,而不是user_id,我想要像author_id? 我怎样才能做到这一点?

trans by 2019-07-09T07:24:14Z

ruby on rails - ActiveRecord数据类型的文档页面在哪里?

我找不到包含所有数据类型列表的活动记录文档页面。

有人可以帮我吗?

trans by 2019-06-03T10:52:37Z

在rails中显示待定迁移

是否有rake任务显示rails应用程序中的挂起迁移?

trans by 2019-05-12T03:22:03Z

ruby on rails - 将时间戳添加到现有选项卡

我需要将时间戳(created_at updated_at)添加到现有表。 我尝试了以下代码,但它没有用。 我也尝试过我在网上找到的其他解决方案,但它们也不起作用。

class AddTimestampsToUser < ActiveRecord::Migration
    def change_table
      add_timestamps(:users)
    end
end

我该怎么做?

trans by 2019-04-26T18:42:34Z

mysql - 回滚失败的Rails迁移

如何回滚失败的rails迁移? 我希望rake db:rollback可以撤消失败的迁移,但不会,它会回滚先前的迁移(失败的迁移减去1)。 并且rake db:migrate:down VERSION=myfailedmigration也不起作用。 我已经碰到了几次,这非常令人沮丧。 这是我为复制问题所做的简单测试:

class SimpleTest < ActiveRecord::Migration
  def self.up
    add_column :assets, :test, :integer
    # the following syntax error will cause the migration to fail
    add_column :asset, :test2, :integer
  end

  def self.down
    remove_column :assets, :test
    remove_column :assets, :test2
  end
end

结果:

==  SimpleTest: migrating =====================================================
-- add_column(:assets, :test, :integer)
   -> 0.0932s
-- add_column(:asset, :error)
rake aborted!
An error has occurred, all later migrations canceled:

wrong number of arguments (2 for 3)

好吧,让我们回滚:

$ rake db:rollback
==  AddLevelsToRoles: reverting ===============================================
-- remove_column(:roles, :level)
   -> 0.0778s
==  AddLevelsToRoles: reverted (0.0779s) ======================================

是吧? 这是我在SimpleTest之前的最后一次迁移,而不是失败的迁移。 (哦,如果迁移输出包含版本号,那将会很好。)

因此,让我们尝试运行失败的迁移SimpleTest:

$ rake db:migrate:down VERSION=20090326173033
$

没有任何事情发生,也没有输出。 但也许它还是进行了迁移? 因此,我们修复SimpleTest迁移中的语法错误,并尝试再次运行它。

$ rake db:migrate:up VERSION=20090326173033
==  SimpleTest: migrating =====================================================
-- add_column(:assets, :test, :integer)
rake aborted!
Mysql::Error: Duplicate column name 'test': ALTER TABLE `assets` ADD `test` int(11)

不。 显然迁移:down不起作用。 它并没有失败,它只是没有执行。

除了手动进入数据库并删除它,然后运行测试之外,没有办法摆脱那个重复的表。 必须有一个比这更好的方法。

trans by 2019-04-21T10:07:39Z

Rails迁移:撤消列的默认设置

我有问题,我在Rails中有一个迁移设置列的默认设置,如下例所示:

def self.up
  add_column :column_name, :bought_at, :datetime, :default => Time.now
end

假设,我想在以后的迁移中删除默认设置,如何使用rails迁移?

我目前的解决方法是在rails迁移中执行自定义sql命令,如下所示:

def self.up
  execute 'alter table column_name alter bought_at drop default'
end

但我不喜欢这种方法,因为我现在依赖于底层数据库如何解释这个命令。 如果数据库发生更改,此查询可能不再起作用,迁移将被破坏。 那么,有没有办法表示在rails中删除列的默认设置?

trans by 2019-03-20T11:29:41Z

1 2 下一页 共2页