I am running rails 1.9.3 and I wanted to add a column to an already migrated table.I tried the following:
Firstly I just edited the migrate file and added my field and ran the rake db:migrate command only to see the following error:
NameError: undefined local variable or method `migrate' for main:Object
from (irb):13
from /Users/praveenmody/.rvm/gems/ruby-1.9.3-p448/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /Users/praveenmody/.rvm/gems/ruby-1.9.3-p448/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /Users/praveenmody/.rvm/gems/ruby-1.9.3-p448/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Next I tried running the following command from the rails console:
rails generate migration add_place_to_coordinates place:string
only to recieve the following error:
NameError: undefined local variable or method `string' for main:Object
from (irb):10
from /Users/praveenmody/.rvm/gems/ruby-1.9.3-p448/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /Users/praveenmody/.rvm/gems/ruby-1.9.3-p448/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /Users/praveenmody/.rvm/gems/ruby-1.9.3-p448/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Here's my migrate file:
class CreateCoordinates < ActiveRecord::Migration
def change
create_table :coordinates do |t|
t.float :lattitude
t.float :longitude
t.timestamps
end
end
end
Don't run the command in the rails console. Run it from the normal shell within the project directory.
A bit late to answer, hope it helps others :
rails generate migration add_price_to_service_elements price:decimal
followed by:
rails db:migrate
Explanation:
'add' is used to add column, 'price' is the column to be added 'to' references the tablename which is 'service_elements'. If you add up text in quotes seperated by an '_' (underscore) you will get above command. price:decimal further specifies the type of values column would be storing in this case decimal values.
rake db:migrate command checks for latest migration version(maintained in schema_migrations table) and in this case executes the migration version generated by the above command.
As already pointed out such command need to be run from console and within the app directory.
Update : Still to make it work properly, files need to be manually updated for changes to reflect on browser :
for eg. controller, view (index,show,_form) etc.
Related
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 have a Rails 4.2 app on Digital Ocean. After deploying a comments model (with a migrate file), it works fine in development, but not in production.
I have been using Capistrano (per this tutorial - https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma) Deploys have been going fine until now.
This was my migrate file (missing from my deploy).
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.references :user, index: true, foreign_key: true
t.text :body
t.references :path, index: true, foreign_key: true
t.timestamps null: false
end
end
end
I checked and can see my schema in both production and dev match and refer to the last reference in the migration files.
But when
When I use rails console production, I can see that the comments table is not there. Here is the message in my console.
Loading production environment (Rails 4.2.5)
2.2.1 :001 > Comment.all
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "comments" does not exist
LINE 1: SELECT "comments".* FROM "comments"
^
: SELECT "comments".* FROM "comments"
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `block in exec_no_cache'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `exec_no_cache'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:584:in `execute_and_clear'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `exec_query'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:355:in `select'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/querying.rb:39:in `find_by_sql'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:639:in `exec_queries'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:515:in `load'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:243:in `to_a'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:630:in `inspect'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:45:in `require'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:45:in `block in exec_app_rails'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:34:in `loop'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:34:in `exec_app_rails'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/cli.rb:5:in `<top (required)>'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/bin/rails:9:in `require'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/bin/rails:9:in `<top (required)>'
from /home/laurie/.rvm/gems/ruby-2.2.1/bin/rails:23:in `load'
from /home/laurie/.rvm/gems/ruby-2.2.1/bin/rails:23:in `<main>'
from /home/laurie/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
from /home/laurie/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'2.2.1 :002 >
I have tried sudo-apt-get clean as well, I have reset my databases, added the action-pack caching gems and restarted my server several times.
My production log lists the comments view files which aren't rendering (because there is no comments table).
Running rake db:migrate:status shows "no file"
up 20151224215110 Devise create users
up 20151231173736 Add first name last name to users
up 20160112022120 Add role to users
up 20160122204744 Create paths
up 20160123004558 Add attachment image to paths
up 20160124191428 Remove user id from paths
up 20160124191601 Add user id to paths
up 20160124212622 Remove bio from users
up 20160124213023 Add bio to users
up 20160124213311 Remove title from paths
up 20160124213624 Add title content to paths
up 20160204222816 ********** NO FILE **********
checking the directory (production server) I see the file is not there.
Also, deleted old migrations, checked my server for memory, etc. Coming up blank?
I know that his is a silly question, but are you using the console with the production environment?
RAILS_ENV=production rails c
(Rails 4.2.4) Hello, beginner here. I am working on a project which does not need a DB or activeRecord. Therefore, when making my Rails project I appended the -O (to disable Active Record and database) (rails new MyApp -O)
I read that to do a model not backed by a database you can just create a file in
app/models/site.rb.
No need to do:
rails generate model Site
So I added my model, which looks something like this:
class Site
attr_reader :name
attr_reader :out_average
attr_reader :in_average
attr_reader :change
def initialize(name, in_average, out_average)
#name = name
#out_average = out_average
#in_average = in_average
#change = find_increase
end
def find_increase()
if #in_average && #out_average != 0
#change = ((#in_average - #out_average)/#out_average)*100
else
#change = 0
end
return #change
end
end
So, I then started up console "rails c" and when I try to invoke a new Site object, I get an error:
irb(main):001:0> Site.new
NameError: uninitialized constant Site
from (irb):1
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
from /home/ms-87/Documents/projects/rails_projects/site_seasonality/bin/rails:8:in `<top (required)>'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
I made sure I started the console from the root of my app. I also made sure to use the proper naming convention (site.rb is the filename in app/model/, "Site" is the name of my class inside the file). Can anyone help me as to why this isn't working? Is my thinking here wrong? Thanks.
My first error was that my filenames were capitalized "Site.rb", I had actually fixed this before I posted. But after I fixed it, I accidentally started using "irb" instead of "rails c". DOH! Sorry for the post.
When loading the Rails console in sandbox as following the tutorial, i can't create a new user object ? I've got this message :
>> User.new
NameError: uninitialized constant User
from (irb):1
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/Genosia/code/sample_app/bin/rails:8:in `<top (required)>'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
You must have a user class in your app/models folder.
You can use the rails generator for that:
bundle exec rails g model user name:string date_of_birth:date ...
That will create the model under app/models and the migration under db/migrate
To run the migrations run
bundle exec rake db:migrate
bundle exec rails c --sandbox
Then it should work.
Note that opening the console in sandbox mode will rollback all the database changes when you close it.
To have persistent changes run
bundle exec rails c
I am sure its a basic problem in RoR but I added a new table called hooods_one_providers. This table has no corresponding model - It should connects two models - Providers and Hoods. I am trying to call it in the console but getting instead - uninitialized constant.
When I run:
ActiveRecord::Base.connection.tables
=> ["schema_migrations", "users", "roles", "users_roles", "providers", "food_items", "food_items_users", "feedbacks", "addresses", "carts", "link_carts", "hoods", "drink_items", "addons_ons", "addons_nears", "customize_foods", "addresses_hoods", "hoods_one_providers"]
I can see the table but I cant read from it. When I run hooods_one_providers i get the uninitialized constant error:
NameError: uninitialized constant HoodsOneProvider
from (irb):14
from /home/ido/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/ido/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/ido/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
In the model provider I have:
has_and_belongs_to_many :hoods_one, class_name: 'HoodsOne'
And in the model hood I have:
has_and_belongs_to_many :providers
Will appreciate any help.
Thanks
This could happen for any one of the cases below
Running reload! should fix.
Exit the console and run console again by rails c
Make sure the model file exists for the respective table. Let's assume your table name is hoods_one_providers then there should be hoods_one_providers.rb file under the model.
if you've added the table while the console is running, the classes are already cached by the console. Running reload! should fix the issue but in case it doesn't, a restart of the console should.
UPDATE:
You are using has_and_belongs_to_many which needs you to manually create the joins table.
UPDATE: creating the joins table
create a migration which contains the following to create the joins table. the id: false option tells it not to create an id column
create_table :hoods_ones_providers, id: false do |t|
t.references :hoods_one, :provider
end
add_index :hoods_ones_providers, [:hoods_one_id, :provider_id]