rails-migrations

What is the best way to drop a table & remove a model in Rails 3?

风格不统一 提交于 2019-11-28 16:10:59
I have a model & a table which I no longer need in my App, I could leave them there but I would like to remove them to keep things tidy. I'm trying to figure out the best way to remove them with out messing around with my migrations & db/schema.rb files & any side effect it could have on my production environment, my app is on Heroku. I'm using PostgreSQL on both my local machine & heroku. So far I've found two ways to do this but am not sure which is the best method/ rails way? Method 1 I thought about just going in to my database & dropping the table & then destroying the model. rails db

has_many, belongs_to relation in active record migration rails 4

你离开我真会死。 提交于 2019-11-28 16:03:46
I have created a User model and later a Task model. I have not mentioned any relation between them while creating. I understand that User has_many Tasks and a Task belongs_to User . I need to establish this relation between them through migration. My question is, what would be the migration generation command for establishing that relation? Any help would be highly appreciated. You could call: rails g model task user:references which will generates an user_id column in the tasks table and will modify the task.rb model to add a belongs_to :user relatonship. Please note, you must to put manually

What is the best way to resolve Rails orphaned migrations?

我们两清 提交于 2019-11-28 15:48:10
问题 I have been switching between branches in a project and each of them have different migrations... This is the scenario: $ rake db:migrate:status Status Migration ID Migration Name -------------------------------------------------- ... up 20130307154128 Change columns in traffic capture up 20130311155109 Remove log settings up 20130311160901 Remove log alarm table up 20130320144219 ********** NO FILE ********** up 20130320161939 ********** NO FILE ********** up 20130320184628 ********** NO

Add timestamps to an existing table

拥有回忆 提交于 2019-11-28 15:45:12
I need to add timestamps ( created_at & updated_at ) to an existing table. I tried the following code but it didn't work. class AddTimestampsToUser < ActiveRecord::Migration def change_table add_timestamps(:users) end end Ben Simpson The timestamp helper is only available in the create_table block. You can add these columns by specifying the column types manually: class AddTimestampsToUser < ActiveRecord::Migration def change_table add_column :users, :created_at, :datetime, null: false add_column :users, :updated_at, :datetime, null: false end end While this does not have the same terse syntax

Why am I asked to run 'rake db:migrate RAILS_ENV=test'?

ぐ巨炮叔叔 提交于 2019-11-28 15:42:51
On Rails 4.0.0.rc1, Ruby 2.0.0, after I run a migration, I see the following error when I try to run a test through rspec : /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!': Migrations are pending; run 'rake db:migrate RAILS_ENV=test' to resolve this issue. (ActiveRecord::PendingMigrationError) That doesn't seem right. No one migrates their test database, do they? They db:test:prepare it, which—to be fair—I've forgotten to do. So I run rake db:test:prepare and run my rspec command again…and see the same

What is the difference between t.belongs_to and t.references in rails?

廉价感情. 提交于 2019-11-28 15:42:03
问题 What is the difference between t.references and t.belongs_to ? Why are we having those two different words? It seems to me they do the same thing? Tried some Google search, but find no explanation. 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 回答1: Looking at the

Rails migrations: Undo default setting for a column

牧云@^-^@ 提交于 2019-11-28 15:31:39
问题 I have the problem, that I have an migration in Rails that sets up a default setting for a column, like this example: def self.up add_column :column_name, :bought_at, :datetime, :default => Time.now end Suppose, I like to drop that default settings in a later migration, how do I do that with using rails migrations? My current workaround is the execution of a custom sql command in the rails migration, like this: def self.up execute 'alter table column_name alter bought_at drop default' end But

Show pending migrations in rails

家住魔仙堡 提交于 2019-11-28 15:11:31
问题 Is there a rake task that shows the pending migrations in a rails app? 回答1: rake db:migrate:status (Rails 3 to 5) or rails db:migrate:status (Rails 5) will accomplish this. See this commit. up means the migration has been run. down means the migration has not been run. 回答2: There is rake db:abort_if_pending_migrations (at least in Rails 2.3.3, not sure when it was introduced). The description says 'Raises an error if there are pending migrations'. This seems to be used more as a prerequisite

How to drop columns using Rails migration

余生长醉 提交于 2019-11-28 14:55:58
What's the syntax for dropping a database table column through a Rails migration? Nick Hammond remove_column :table_name, :column_name For instance: remove_column :users, :hobby would remove the hobby Column from the users table. prabu For older versions of Rails ruby script/generate migration RemoveFieldNameFromTableName field_name:datatype For Rails 3 and up rails generate migration RemoveFieldNameFromTableName field_name:datatype Powers Rails 4 has been updated, so the change method can be used in the migration to drop a column and the migration will successfully rollback. Please read the

Where is the documentation page for ActiveRecord data types?

最后都变了- 提交于 2019-11-28 14:33:04
问题 I can't find the active record documenation page that has a list of all the data types. Can someone help me out? 回答1: If you're talking about the types for migrations, e.g. string, integer, datetime, etc, then you want ActiveRecord::ConnectionAdapters::TableDefinition, the column method. (Rails 5 edit: see also connection.add_column.) As of this update, the standard types are: :primary_key :string :text :integer :bigint :float :decimal :numeric :datetime :time :date :binary :boolean The