I'm trying to migrate the rails app but it fail. the issue in the paperclip gem migration.
I tried to use both way for migration but both of them not work properly:
class AddAttachmentImageToItems < ActiveRecord::Migration[6.1]
def self.up
add_attachment :items, :image
end
def self.down
remove_attachment :items, :image
end
end
also tried:
class AddAttachmentImageToItems < ActiveRecord::Migration[6.1]
def change
add_attachment :items, :image
end
end
after run rails db:migrate showing error:
/Users/zi/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/pry-byebug-3.8.0/lib/pry-byebug/control_d_handler.rb:5: warning: control_d_handler's arity of 2 parameters was deprecated (eval_string, pry_instance). Now it gets passed just 1 parameter (pry_instance)
== 20210713231704 AddAttachmentImageToItems: migrating ========================
-- add_attachment(:items, :image)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
wrong number of arguments (given 4, expected 3)
/Users/zi/rails/stage_1/db/migrate/20210713231704_add_attachment_image_to_items.rb:3:in `up'
/Users/zi/rails/stage_1/bin/rails:5:in `<top (required)>'
/Users/zi/rails/stage_1/bin/spring:10:in `block in <top (required)>'
/Users/zi/rails/stage_1/bin/spring:7:in `<top (required)>'
Caused by:
ArgumentError: wrong number of arguments (given 4, expected 3)
/Users/zi/rails/stage_1/db/migrate/20210713231704_add_attachment_image_to_items.rb:3:in `up'
/Users/zi/rails/stage_1/bin/rails:5:in `<top (required)>'
/Users/zi/rails/stage_1/bin/spring:10:in `block in <top (required)>'
/Users/zi/rails/stage_1/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Found the issue in the ruby version, downgrade to 2.6.8 and migration work properly.
Related
I do not know why this is breaking. I'm trying to run a migration on some code I've inherited and I've hit a snag. Here is the error message followed by the migration file code.
== AddStorecreditGizmoType: migrating ========================================
rake aborted!
NoMethodError: undefined method `find_by_name' for GizmoCategory(id: integer, description: string):Class
/var/lib/gems/1.9.1/gems/activerecord-2.3.14/lib/active_record/base.rb:1876:in `method_missing'
/home/thefonso/site-dev/vendor/plugins/will_paginate/lib/will_paginate/finder.rb:175:in `method_missing_with_paginate'
db/migrate//20090628000954_add_storecredit_gizmo_type.rb:4:in `up'
/var/lib/gems/1.9.1/gems/activerecord-2.3.14/lib/active_record/migration.rb:282:in `block in migrate'
/var/lib/gems/1.9.1/gems/activerecord-2.3.14/lib/active_record/migration.rb:282:in `migrate'
/var/lib/gems/1.9.1/gems/activerecord-2.3.14/lib/active_record/migration.rb:365:in `migrate'
/var/lib/gems/1.9.1/gems/activerecord-2.3.14/lib/active_record/migration.rb:457:in `run'
/var/lib/gems/1.9.1/gems/activerecord-2.3.14/lib/active_record/migration.rb:409:in `run'
/var/lib/gems/1.9.1/gems/rails-2.3.14/lib/tasks/databases.rake:135:in `block (3 levels) in <top (required)>'
/var/lib/gems/1.9.1/gems/rake-11.3.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:migrate:up
(See full trace by running task with --trace)
Here is the migration file code
class AddStorecreditGizmoType < ActiveRecord::Migration
def self.up
# TODO: GizmoCategory.find_by_name("misc") is breaking...why? Attempted to replace with "where" but same error.
new = GizmoType.new(:name => "store_credit", :description => "Store Credit", :gizmo_category => GizmoCategory.find_by_name("misc"), :required_fee_cents => 0, :suggested_fee_cents => 0)
new.save!
DB.execute("UPDATE gizmo_contexts_gizmo_types SET gizmo_type_id = #{new.id} WHERE gizmo_type_id IN (SELECT id FROM gizmo_types WHERE name = 'gift_cert');")
end
def self.down
DB.execute("UPDATE gizmo_contexts_gizmo_types SET gizmo_type_id = (SELECT id FROM gizmo_types WHERE name = 'gift_cert') WHERE gizmo_type_id IN (SELECT id FROM gizmo_types WHERE name = 'store_credit');")
GizmoType.find_by_name("store_credit").destroy
end
end
I've tried using "where" and "Find_by" but I keep getting this same "undefined method blahblah for GizmoCategory
Can you point me in the right direction? What am I missing? forgetting? What's happening here?
Oh and versions are as follows
rails - 2.3.14
ruby - 1.9.3p194
Thanks
The find_by_* methods didn't exist in activerecord 2.3.14. Use Model.find instead.
category = GizmoCategory.find(:first, conditions: "name = 'store_credit'")
category.destroy if category
I am using the gem blamer to add a userstamp into some tables that are already created. I tried to add userstamps while creating a new table and it worked.But I need to add to existing tables and I don't know what to write in the migration. This gem is like others that add userstamp but only this one seems working in rails 6.
class User < ActiveRecord::Base
cattr_accessor :current_user
end
class ApplicationController < ActionController::Base
before_action :set_userstamp
def set_userstamp
User.current_user = User.find(session[:user_id])
end
end
class AddUserstampsToIllustrators < ActiveRecord::Migration[6.0]
def change
add_column :illustrators, :userstamps
end
end
I got the following error:
== 20201106193035 AddUserstampsToIllustrators: migrating ======================
-- add_column(:illustrators, :userstamps)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
wrong number of arguments (given 2, expected 3)
/home/morganaborges/code/MorganaTBorges/bamboleio_project/db/migrate/20201106193035_add_userstamps_to_illustrators.rb:3:in `change'
/home/morganaborges/code/MorganaTBorges/bamboleio_project/bin/rails:9:in `<top (required)>'
/home/morganaborges/code/MorganaTBorges/bamboleio_project/bin/spring:15:in `<top (required)>'
./bin/rails:3:in `load'
./bin/rails:3:in `<main>'
Caused by:
ArgumentError: wrong number of arguments (given 2, expected 3)
/home/morganaborges/code/MorganaTBorges/bamboleio_project/db/migrate/20201106193035_add_userstamps_to_illustrators.rb:3:in `change'
/home/morganaborges/code/MorganaTBorges/bamboleio_project/bin/rails:9:in `<top (required)>'
/home/morganaborges/code/MorganaTBorges/bamboleio_project/bin/spring:15:in `<top (required)>'
./bin/rails:3:in `load'
./bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Looking at https://github.com/infused/blamer , by default the two columns are created_by and updated_by with type integer. So...
add_column :illustrators, :created_by, :integer
add_column :illustrators, :updated_by, :integer
(integer being used in https://github.com/infused/blamer/blob/master/lib/blamer/userstamp.rb#L47 )
ArgumentError: wrong number of arguments (given 2, expected 3) you pass two arguments but you need to pass column type as the third argument. Here is the documentation with a list of types.
add_column :illustrators, :userstamps, :string
# or
add_column :illustrators, :userstamps, :datetime
# etc.
Take a look at the examples section.
Trying to get an app up and running on a new machine. May be using a slightly updated version of Ruby (2.3.4 vs 2.3.1) and the Rails version is 5.1.3.
Here's the migration:
class AddDragAndDropRules < ActiveRecord::Migration[5.0]
def change
add_column :products, :fixture_location, :string, default: "none"
add_column :products, :attaches_to, :uuid, array: true, default: []
end
end
The issue during rails db:migrate.
Error:
== 20160928162420 AddDragAndDropRules: migrating ==============================
-- add_column(:products, :fixture_location, :string, {:default=>"none"})
-> 0.0142s
-- add_column(:products, :attaches_to, :uuid, {:array=>true, :default=>[]})
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
no implicit conversion of Array into String
/Users/mike/api/db/migrate/20160928162420_add_drag_and_drop_rules.rb:4:in `change'
bin/rails:9:in `require'
bin/rails:9:in `<main>'
TypeError: no implicit conversion of Array into String
/Users/mike/api/db/migrate/20160928162420_add_drag_and_drop_rules.rb:4:in `change'
bin/rails:9:in `require'
bin/rails:9:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Anyone able to spot the error?
In case this helps anyone else - the project was upgraded to Rails 5.1.3 at some point which is causing UUID array migrations to fail due to a bug.
Issue is here (https://github.com/rails/rails/issues/30539) and workaround in 5.1.2-5.1.3 is to do this:
add_column :products, :attaches_to, :uuid, array: true, default: '{}'
Should be fixed in 5.1.4 and above.
I installed a gem acts-as-taggable-on whenever i tried to run rails db:migrate it would output this:
C:\Sites\novosti>rails db:migrate
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:
class ActsAsTaggableOnMigration < ActiveRecord::Migration[4.2]
C:/Sites/novosti/db/migrate/20170708123900_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:2:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
StandardError: Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:
class ActsAsTaggableOnMigration < ActiveRecord::Migration[4.2]
C:/Sites/novosti/db/migrate/20170708123900_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:2:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
So i just added [5.1] and it fixed the problem but another one appeared
C:\Sites\novosti>rails db:migrate
== 20170708123900 ActsAsTaggableOnMigration: migrating ========================
-- create_table(:tags)
-> 0.0028s
-- create_table(:taggings)
-> 0.0033s
-- add_index(:taggings, :tag_id)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
Index name 'index_taggings_on_tag_id' on table 'taggings' already exists
C:/Sites/novosti/db/migrate/20170708123900_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:23:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
ArgumentError: Index name 'index_taggings_on_tag_id' on table 'taggings' already exists
C:/Sites/novosti/db/migrate/20170708123900_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:23:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Index name on a table should be unique. So this is an error from db as the the index already exists. Please drop the index on the table of the db before migration or you can remove the add_index in migration if the index is already ok in the table
I am working a new project in Codeanywhere (I am new on Ruby), but when I try to use "rake db : migrate" I have this error:
rake aborted!
Don't know how to build task ':' (see --tasks)
/home/cabox/.rvm/gems/ruby-2.1.2/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
(See full trace by running task with --trace)
Before this I use bundle update and after and nothing, and when I put "rake-T", db Migartion is on the tasks, when I try with --trace, pass the same.
I try before with rake db:migrate and the same happened
`rake aborted!
SyntaxError:
/home/cabox/workspace/blog/db/migrate/20170103233409_create_posts.rb:5: syntax error, unexpected ':'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:761:in `load_migration'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord- 4.1.6/lib/active_record/migration.rb:757:in `migration'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:752:in `disable_ddl_transaction'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:1044:in `use_transaction?'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:1036:in `ddl_transaction'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:990:in `execute_migration_in_transaction'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:952:in `block in migrate'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:948:in `each'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:948:in `migrate'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:807:in `up'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:785:in `migrate'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)` >
Does anyone know how to solve this problem?
In the migration file I have:
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.strind :
t.text :body
t.timestamps
end
end
end
I am pretty sure what is happening is you are inserting a space in there when you type it out. It needs to be
rake db:migrate
not
rake db : migrate
That error message is pretty common when there is a spelling error or some other kind of typing error when running the command.
Your migration has a typo as well, should be this
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.text :body
t.timestamps
end
end
end
Rake is a software task management tool, similar to Make, etc. in other systems
The generic syntax is
rake namesapce:task
In your case it is
rake db:migrate
Try doing rake db:reset if you want to rebuild your db again. It does 4 things for you
rake db:drop
rake db:create
rake db:migrate
rake db:seed