undefined local variable or method `acts_as_gmappable' - google-maps

I created a Heroku app which utilizes the gmaps4fails gem for one of the application's features.
When I tried to seed the database, I get
rake aborted!
undefined local variable or method 'acts_as_gmappable' for #<Class:0x00000004b5b928>
Any ideas how to fix this?
Everything works fine when testing locally.
Edit: Here's my model code
class Hall < ActiveRecord::Base
has_many :hall_features
has_many :green_features, :through => :hall_features
has_many :settings, :through => :pinned_halls
belongs_to :operational_unit
acts_as_gmappable
...
Migration:
class CreateHalls < ActiveRecord::Migration
def up
create_table 'halls' do |t|
t.string 'name'
t.text 'background'
t.text 'energy_info'
t.float 'latitude'
t.float 'longitude'
t.boolean 'gmaps'
t.timestamps
end
end
def down
drop_table 'halls' # deletes the whole table and all its data!
end
end
Here's the error from my terminal: http://i48.tinypic.com/zvoggg.png

Ugh .. put the gem under :development in Gemfile only. Including it in :production solved everything.
I'll be crying myself to sleep tonight. Some tears of happiness too.

Related

Rails change_column removing index from schema

I am trying to make it so certain columns cannot be null. I use change_column no problem but some of these columns have an index attached to them signifying that it is unique.
However when I run this migration:
change_column :users, :username, :string, null: false
change_column :users, :email, :string, null: false
change_column :users, :password, :string, null: false
change_column :users, :terms_agreed, :boolean, null: false
It removes the add_index in the schema
schema.rb
- add_index "users", ["username"], :name => "index_users_on_username_code", :unique => true
- add_index "users", ["email"], :name => "index_users_on_email_code", :unique => true
add_index "users", ["confirmation_code"], :name => "index_users_on_confirmation_code", :unique => true
How do I do this without removing the indexes?
P.S. its not actually removing the indexes in the database. Just in the schema.rb file.
The behaviour of how migrations are treated depends on you database implementation. More info here, but in your migration you should explicitly request an index in your change.
class DoSomethingToTable < ActiveRecord::Migration
def change
change_column :users, :username, :string, null: false, index: true
end
end
See the docs for more information.
So still unclear as to what happened, but turns out that the indexes got deleted from the database during some database manipulation.
I just put the schema back the way I needed it to be and ran rake db:setup then my migration again and everything worked fine. Very confusing and a lot of unnecessary time spent trying to figure it out.

Ruby on Rails - rake db:migrate is not working

I am using def change for models. Here is migration output of my models:
$ rake db:migrate
== 20150802221545 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.3319s
== 20150802221545 CreateUsers: migrated (0.3322s) =============================
== 20150802221550 CreatePages: migrating ======================================
-- create_table(:pages)
-> 0.2457s
-- add_index(:pages, :subject_id)
-> 0.2487s
-- add_index(:pages, :permalink)
-> 0.2464s
== 20150802221550 CreatePages: migrated (0.7416s) =============================
== 20150802221558 CreateSubjects: migrating ===================================
-- create_table(:subjects)
-> 0.2453s
== 20150802221558 CreateSubjects: migrated (0.2456s) ==========================
== 20150802221603 CreateSections: migrating ===================================
-- create_table(:sections)
-> 0.2776s
-- add_index(:sections, :page_id)
-> 0.2484s
== 20150802221603 CreateSections: migrated (0.5265s) ==========================
Now, I created a migration named AlterUsers and wrote following code.
- class AlterUsers < ActiveRecord::Migration
def change
reversible do |dir|
dir.up {
rename_table :users, :admin_users
add_column :admin_users, :username, :string, :limit => 25
#add_column("admin_users", "username", :string, :limit => 25)
change_column :admin_users, :email, :string, :limit => 100
#change_column("admin_users", "email", :string, :limit => 100)
rename_column :admin_users, :password, :hashed_password
#rename_column("admin_users", "password", "hashed_password")
add_column :admin_users, :salt, :string, :limit => 40
#add_column("admin_users", "salt", :string, :limit => 40)
puts "*** about to add index ***"
add_index :admin_users, :username
#add_index("admin_users", "username")
}
dir.down{
remove_index :admin_users, :username
remove_column :admin_users, :salt
rename_column :admin_users, :hashed_password, :password
change_column :admin_users, :email, :string, :default=> "", :null => false
remove_column :admin_users, :username, :string, :limit => 25
raname_table :admin_users, :users
}
end
end
end
I used def change as I am using rails4. But rake db:migrate doesn't affect database. Then I wrote same code as instructor's code:
class AlterUsers < ActiveRecord::Migration
def self.up
rename_table :users, :admin_users
add_column :admin_users, :username, :string, :limit => 25
change_column :admin_users, :email, :string, :limit => 100
rename_column :admin_users, :password, :hashed_password
add_column :admin_users, :salt, :string, :limit => 40
puts "*** about to add index ***"
add_index :admin_users, :username
end
def self.down
remove_index :admin_users, :username
remove_column :admin_users, :salt
rename_column :admin_users, :hashed_password, :password
change_column :admin_users, :email, :string, :default=> "", :null => false
remove_column :admin_users, :username, :string, :limit => 25
raname_table :admin_users, :users
end
end
It worked. After all these migrations, I am trying to undo by using this command rake db:migrate VERSION=0. But it's not working. Here is db migration status:
$ rake db:migrate:status
database: mysqlapp_development
Status Migration ID Migration Name
--------------------------------------------------
up 20150802221545 Create users
up 20150802221550 Create pages
up 20150802221558 Create subjects
up 20150802221603 Create sections
up 20150802221735 Alter users
And here is rake db:migrate --trace output
$ rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:dump
Now I have following major queries in addition to above problem.
1) In which cases rake db:migrate doesn't affect? (I'm confused because of it's working behaviour)
2) How to redo the undo/roll back migrations?
You should simply split this into several migrations. Just use change and let rails deal with creating the reverse migration.
Given:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :password
t.timestamps null: false
end
end
end
Migrations:
class ChangeUsersTable < ActiveRecord::Migration
def change
rename_table :users, :admin_users
end
end
class AddHashedPasswordToAdminUsers < ActiveRecord::Migration
def change
# you should not just migrate the plaintext passwords!
remove_column :admin_users, :password, :string
add_column :admin_users, :password_hash, :string
add_column :admin_users, :salt, :string, limit: 40
end
end
class AddUserNameToAdminUser < ActiveRecord::Migration
def change
add_column :admin_users, :username, :string
end
end
Result:
ActiveRecord::Schema.define(version: 20150802210303) do
create_table "admin_users", force: :cascade do |t|
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_hash"
t.string "salt", limit: 40
t.string "username"
end
end
Don't roll your own password encryption!
I would also recommend that you use has_secure_password rather than rolling your own password encryption. Its way to easy to mess it up.
In that case the migration would be:
class AddPasswordDigestToAdminUsers < ActiveRecord::Migration
def change
# you should not just migrate the plaintext passwords!
remove_column :admin_users, :password, :string
add_column :admin_users, :password_digest, :string
# has_secure_password concatenates the salt into the digest.
# no salt column needed.
end
end

rake db:migrate not creating table

I'm just following this RoR tut, I'm doing it the same way but I'm stuck creating a table:
$ rails generate model User
invoke active_record
create db/migrate/20140718180319_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
This is my xxxxx_create_users.rb
class CreateUsers < ActiveRecord::Migration
def Up
create_table :users do |t|
t.column "first_name", :string, :limit => 25
t.string "last_name", :limit => 50
t.string "email", :default => "", :null => false
t.string "password", :limit => 40
t.timestamps
end
end
def down
drop_table :users
end
end
When I run db:migrate the table is not being created:
$ rake db:migrate
== 20140718182504 CreateUsers: migrating ======================================
== 20140718182504 CreateUsers: migrated (0.0000s) =============================
Is missing
create_table(:users)
-> x.xxxxxs
What am I doing wrong? Thanks.
Isn't it a typo with your "Up" migration method? Try with:
def up
instead of:
def Up

Why is this migration not migrating on mysql

I am integrating a CAS client using rack-cas gem on an application using mysql database, When i run,
rails generate cas_session_store_migration as specified on the readme, it generates a migration 20140428130031_create_rack_cas_sessions.rb which looks like this:
class CreateRackCasSessions < ActiveRecord::Migration
def self.up
create_table :sessions do |t|
t.string :session_id, :null => false
t.string :cas_ticket
t.text :data
t.timestamps
end
add_index :sessions, :session_id
add_index :sessions, :cas_ticket
add_index :sessions, :updated_at
end
def self.down
drop_table :sessions
end
end
when i run rake db:migrate or bundle exec rake db:migrate nothing happens. The migration fails to take place and there is no error message(s). The same migration on another service using sqlite database migrates successfully.

Table doesn't exist: SHOW KEYS

I am getting this error when I run db:migrate
Mysql2::Error: Table 'sample_app_development.microposts' doesn't exist: SHOW KEYS FROM
`microposts
This is my migration
class CreateMicroposts < ActiveRecord::Migration
def change
create_table :microposts do |t|
t.string :content
t.integer :user_id
t.timestamps
add_index :microposts, [:user_id, :created_at]
end
end
end
I have tried restarting mysql and deleting and recreating the database.
Move your add_index out of the create_table's block.