rails-migrations

Rails 3.1: can't write to column in same migration that adds it

半城伤御伤魂 提交于 2019-12-02 23:50:29
I had an add_column migration that would run fine. However, after running it and firing up a console, I would find the first_name and last_name columns completely empty. I tried using save! instead and it had the same effect--no errors reported. Here's the original: class UserAddFirstNameAndLastName < ActiveRecord::Migration def change # add column first name, last name string add_column :users, :first_name, :string add_column :users, :last_name, :string User.all.each do |u| u.first_name = 'first name' u.last_name = 'last name' u.save end end end I also thought this might be some class loading

Delete old migrations files in a Rails app

点点圈 提交于 2019-12-02 22:48:25
Is it permissible to delete (or archive) old migration files in a Rails app if the schema is stable? My migrations are numerous and I suspect there may be some problem in there somewhere, because I occasionally have problems migrating the database on Heroku. Zack Xu You don't need to keep around your old migration files in a Rails app, because your database schema should be captured either in schema.rb or an equivalent SQL file that can be used to regenerate your schema. Migrations are not the authoritative source for your database schema. That role falls to either db/schema.rb or an SQL file

Best SQL indexes for join table

╄→гoц情女王★ 提交于 2019-12-02 21:44:12
With performance improvements in mind, I was wondering if and which indexes are helpful on a join table (specifically used in a Rails 3 has_and_belongs_to_many context). Model and Table Setup My models are Foo and Bar and per rails convention, I have a join table called bars_foos . There is no primary key or timestamps making the old fields in this table bar_id:integer and foo_id:integer . I'm interested in knowing which of the following indexes is best and is without duplication: A compound index: add_index :bars_foos, [:bar_id, :foo_id] Two indexes A. add_index :bars_foos, :bar_id B. add

Destroy/Remove database in Rails

时间秒杀一切 提交于 2019-12-02 20:35:41
Is it possible to completely remove the database and all migration records etc from an existing application so I can redesign the database from scratch? Paulo Fidalgo By issuing rake -T you have the following database tasks: rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config) rake db:drop # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases) rake db:fixtures:load # Load fixtures into the current environment's database rake db:migrate #

What's the correct syntax for remove_index in a Rails 3.1.0 migration?

半腔热情 提交于 2019-12-02 17:49:14
I'm in the process of adding Devise to an existing Rails app, with a Users table already defined. The devise generator pushed out the following migration: class AddDeviseToUsers < ActiveRecord::Migration def self.up change_table(:users) do |t| ## Database authenticatable t.string :email, :null => false, :default => "" t.string :encrypted_password, :null => false, :default => "" ## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at ## Rememberable t.datetime :remember_created_at ## Trackable t.integer :sign_in_count, :default => 0 blah blah blah.... end add_index

Managing mongoid migrations

不羁的心 提交于 2019-12-02 16:40:59
Can someone give me a short introduction to doing DB migrations in Rails using Mongoid? I'm particularly interested in lazy per document migrations. By this, I mean that whenever you read a document from the database, you migrate it to its latest version and save it again. Has anyone done this sort of thing before? I've come across mongoid_rails_migrations , but it doesn't provide any sort of documentation, and although it looks like it does this, I'm not really sure how to use it. I should point out I'm only conceptually familiar with ActiveRecord migrations. If you want to do the entire

Ruby on Rails: adding columns to existing database

早过忘川 提交于 2019-12-02 14:56:38
I'm getting an error: SQLite3::SQLException: no such column: ideas.list_id: SELECT "ideas".* FROM "ideas" WHERE "ideas"."list_id" = 2 But I added t.integer :list_id to my db migration file: class CreateIdeas < ActiveRecord::Migration def change create_table :ideas do |t| t.string :name t.text :description t.string :picture t.timestamps end add_foreign_key :ideas, :lists end end which gave me this: class CreateIdeas < ActiveRecord::Migration def change create_table :ideas do |t| t.string :name t.text :description t.string :picture t.integer :list_id t.timestamps end add_foreign_key :ideas,

How can i remove a column from table using rails console

倖福魔咒の 提交于 2019-12-02 14:49:25
It is easily possible to remove a column using rails migration. class SomeClass < ActiveRecord::Migration def self.up remove_column :table_name, :column_name end end I want to know if there is any way to remove a column from table using console. You can run the codes in up method directly in rails console : >> ActiveRecord::Migration.remove_column :table_name, :column_name If you already have a migration file such as " db/migrate/20130418125100_remove_foo.rb ", you can do this: >> require "db/migrate/20130418125100_remove_foo.rb" >> RemoveFoo.up If you just want to do rake db:migrate , try

Check if a table exists in Rails

守給你的承諾、 提交于 2019-12-02 13:55:58
I have a rake task that won't work unless a table exists. I'm working with more than 20 engineers on a website so I want to make sure they have migrated the table before they can do a rake task which will populate that respective table. Does AR have a method such as Table.exists ? How can I make sure they have migrated the table successfully? In Rails 5 the API became explicit regarding tables/views , collectively data sources . # Tables and views ActiveRecord::Base.connection.data_sources ActiveRecord::Base.connection.data_source_exists? 'kittens' # Tables ActiveRecord::Base.connection.tables

heroku run rake db:migrate error

半世苍凉 提交于 2019-12-02 07:33:56
I want do run migration on my app that I have on heroku but I get this error: Running `rake db:migrate` attached to terminal... up, run.1 DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3