when trying to run migration to add a spatial index, get
Unknown key: spatial/Users/ME/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/core_ext/hash/keys.rb:70:in `block in assert_valid_keys'
Using
Ruby 2.0.353
Rails 4.0.2
RGEO 0.3.20
RGEO Active Record 0.4.6
List item
activerecord-mysql2spatial-adapter 0.4.3
Migration Index file looks like
class CreateAddresses < ActiveRecord::Migration
def change
create_table :addresses, :options => 'ENGINE=MyISAM' do |t|
t.string :street_1
t.string :street2
t.string :city
t.string :state
t.string :zip
t.string :country
t.string :full_address
t.column :latlon, :point, :null => false
t.timestamps
end
add_index :addresses, :latlon, :spatial => true
end
end
UPDATE
Corrected this and other errors when I changed the adapter in my database.yml file from mysql2 to mysql2spatial
Related
Followed the tutorial at SitePoint
for a simple sorcery app. I am receiving undefined method `name' for nil:NilClass
migration
class SorceryCore < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email, :null => false
t.string :crypted_password
t.string :salt
t.timestamps :null => false
end
add_index :users, :email, unique: true
end
end
/home/david/Magical/app/views/user_mailer/activation_needed_email.html.erb
<p>Welcome, <%= #user.name %>!</p>
<p>To login to the site, just follow <%= link_to 'this link',
activate_user_url$
<p>Thanks for joining and have a great day!</p>
users.rb
class User < ActiveRecord::Base
authenticates_with_sorcery!
validates :password, length: { minimum: 3 }
validates :password, confirmation: true
validates :email, uniqueness: true, email_format: { message: 'has
invalid format'
End
I took the long way home and cloned the Github repo. At first I was getting a 'stack level too deep' error, but I found that the gemfile loaded rails 4.2.1 .
I changed this to 4.2.10 and all went well after the migrations and minor tweaks in the tutorial.
along the way, I found rails_db gem which is a great simple gem for working with sqLite. rails_db gem
Here is my schema:
create_table :policies do |t|
t.string :name
t.timestamps
end
create_table :permissions do |t|
t.string :name
t.string :method
t.string :controller
t.timestamps
end
create_join_table :policies, :permissions do |t|
t.index :policy_id
t.index :permission_id
end
And here is the code I am using to create the records and their associations:
policy = Policy.create! name: "View All"
permission = Permission.create!({
name: "View Events",
method: "index",
controller: "Events"
})
policy.permissions << permission
And its returning the following error:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "permission_policies" does not exist
The table name that was created through the migration is policies_permissions
I wonder if this is an issue with the class names not being inferred properly
Swap the positions of permissions and policies in the migration so that you create permissions_policies instead of policies_permissions. Rails infers the class names for a join table in alphabetical order.
See this answer for more information on join table naming conventions.
I'm new to Ruby on Rails and would be grateful for some help with migrations.
Version 4
first migration
class CreateRooms < ActiveRecord::Migration
def change
create_table :rooms do |t|
t.integer :legacy_id, null: true
t.string :tag, null: false
t.string :name_en, null: false
t.string :name_et, null: false
t.string :name_ru, null: false
t.string :color, default: 'Black'
t.timestamp :enabled_from, default: 0
t.timestamp :enabled_to, default: 0
t.timestamps null: false
end
add_index :rooms, :tag, unique: true
add_index :rooms, :name_en, unique: true
add_index :rooms, :name_et, unique: true
add_index :rooms, :name_ru, unique: true
end
end
next one
class AddDefaultValuesToRoom < ActiveRecord::Migration
def change
add_column :rooms, :priority, :integer, null: false
change_column :rooms, :enabled_from, :datetime, default: '2008-08-15'
change_column :rooms, :enabled_to, :datetime, default: '2050-12-31'
add_index :rooms, :priority, unique: true
end
end
Strangely enough this is not reflected in schema
create_table "rooms", force: :cascade do |t|
t.integer "legacy_id"
t.string "tag", null: false
t.string "name_en", null: false
t.string "name_et", null: false
t.string "name_ru", null: false
t.string "color", default: "Black"
t.datetime "enabled_from", default: '2008-08-15 00:00:00'
t.datetime "enabled_to", default: '2050-12-31 00:00:00'
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "rooms", ["name_en"], name: "index_rooms_on_name_en", unique: true
add_index "rooms", ["name_et"], name: "index_rooms_on_name_et", unique: true
add_index "rooms", ["name_ru"], name: "index_rooms_on_name_ru", unique: true
add_index "rooms", ["tag"], name: "index_rooms_on_tag", unique: true
rake db:rollback crashes
rake db:rollback STEP=2 --trace
** Invoke db:rollback (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:rollback
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to AddDefaultValuesToRoom (20160127121254)
(0.1ms) begin transaction
== 20160127121254 AddDefaultValuesToRoom: reverting ===========================
(0.1ms) rollback transaction
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
ActiveRecord::IrreversibleMigration/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/migration/command_recorder.rb:65:in `inverse_of'
Now I'm in a fix, could somebody help me out ?
TIA
Pavel
change_column creates an irreversible change to your database, which means you can't roll it back. See the guide: http://guides.rubyonrails.org/active_record_migrations.html#changing-columns
The idea here is that reverting your data would result in data loss, so Rails prevents you from doing something destructive by default and throws an error to stop the process.
Change your last migration to utilize reversible or up/down methods, in order to explicitly tell Rails how to reverse your change (note that you will lose any data stored in the new column priority). See: http://guides.rubyonrails.org/active_record_migrations.html#using-reversible
Here's an example using up/down methods:
class AddDefaultValuesToRoom < ActiveRecord::Migration
def up
add_column :rooms, :priority, :integer, null: false
change_column :rooms, :enabled_from, :datetime, default: '2008-08-15'
change_column :rooms, :enabled_to, :datetime, default: '2050-12-31'
add_index :rooms, :priority, unique: true
end
def down
remove_column :rooms, :priority
change_column :rooms, :enabled_from, :datetime, default: 0
change_column :rooms, :enabled_to, :datetime, default: 0
end
end
I'm implementing a Rails 4 app that has a Reflexive Many-to-Many Association to represent some MailChimp and HelpScout API parameters.
Some of those parameters have child parameters. Thus, those child parameters have parent parameters (otherwise they couldn't be a child, right?! ;D ).
To implement the reflexive association the two tables below were created
create_table "api_params", force: true do |t|
t.string "name"
t.string "description"
t.string "type"
t.boolean "required"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "nested_params", force: true do |t|
t.integer "parent_param_id"
t.integer "child_param_id"
t.boolean "required"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "nested_params", ["child_param_id"], name: "index_nested_params_on_child_param_id"
add_index "nested_params", ["parent_param_id"], name: "index_nested_params_on_parent_param_id"
What I'd like to have are two methods. One to retrieve a database record's parents and another one to retrieve its children. Below I show you an example.
apiParam = ApiParam.first # retrieving any database record
apiParam.parent_params
# => returns a set of apiParam's parents
apiParam.child_params
# => returns a set of apiParam's children
I read all day long about associations but the examples are always the same. I mean, there is always a class where you define a has_many through and another one where you define a belongs_to but it wasn't enough for what I need.
Thanks in advance. I appreciate any kind of help.
I have your solution (in my case I have the model SyncType):
class SyncType < ActiveRecord::Base
has_and_belongs_to_many(:parents,
:class_name => "SyncType",
:join_table => "sync_type_parents",
:foreign_key => "sync_type_id",
:association_foreign_key => "sync_type_parent_id")
has_and_belongs_to_many(:children,
:class_name => "SyncType",
:join_table => "sync_type_parents",
:foreign_key => "sync_type_parent_id",
:association_foreign_key => "sync_type_id")
end
migration:
create_table "sync_type_parents", :force => true, :id => false do |t|
t.integer "sync_type_id", :null => false
t.integer "sync_type_parent_id", :null => false
end
Enjoy! ;)
I found the answer and I hope it can be helpful for everyone with same kind of problem.
In short, I've got the answer from Ryan Bates' railscasts.com website. More specificly, the answer were found here.
My classes are now as follow:
class NestedParam < ActiveRecord::Base
belongs_to :api_param
belongs_to :child_param, class_name: 'ApiParam'
end
class ApiParam < ActiveRecord::Base
has_many :nested_params
has_many :child_params, :through => :nested_params
has_many :parent_api_params, :class_name => "NestedParam", :foreign_key => "child_param_id"
has_many :parent_params, :through => :parent_api_params, :source => :api_param
end
ActiveRecord::Schema.define(version: 20140910175658) do
create_table "api_params", force: true do |t|
t.string "name"
t.string "description"
t.boolean "required", default: true
t.boolean "struct", default: false
t.string "type"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "nested_params", force: true do |t|
t.integer "api_param_id"
t.integer "child_param_id"
t.boolean "required"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "nested_params", ["api_param_id"], name: "index_nested_params_on_api_param_id"
add_index "nested_params", ["child_param_id"], name: "index_nested_params_on_child_param_id"
end
I am using Rails 4.1.1 and pg (0.17.1) gem I having error while running the migration
ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: column "page_ids" is of type integer[] but default expression is of type integer
here is my migration code
class CreatePages < ActiveRecord::Migration
def change
create_table :pages do |t|
t.string :name
t.integer :page_ids, array: true, null: false, default: '{}'
t.timestamps
end
end
end
the array: true not working
Try:
t.integer :page_ids, array: true, null: false, default: []