How would I go about changing the decimals and length attributes of a float column in my Rails 3 migration file. I have tried the following w/ no success:
class IncreaseLatitudeLongitudeFieldLengths < ActiveRecord::Migration
def self.up
change_column :skateparks, :latitude, :float, {:length => 15, :decimals => 12}
change_column :skateparks, :longitude, :float, {:length => 15, :decimals => 12}
end
def self.down
change_column :skateparks, :latitude, :float, {:length => 0, :decimals => 0}
change_column :skateparks, :longitude, :float, {:length => 0, :decimals => 0}
end
end
Personal experience what works best (since MySQL/sqlite sometimes refuses changes to columns): Create a new column, copy the data, delete the old column, rename the new column.
# Example for latitude
add_column :skateparks, :latitude2, :decimal, :precision => 15, :scale => 12
execute "UPDATE skateparks SET latitude2 = latitude"
remove_column :skateparks, :latitude
rename_column :skateparks, :latitude2, :latitude
EDIT: On the second look :float, { :length => 15, :decimals => 12 } seems to be wrong. I assume you meant: :decimal, :precision => 15, :scale => 12?
Related
I'm trying to remove :precision and :scale from my rake database, so it should look like default decimal like this: t.decimal "results", :default => 0.0
here is mine:
t.decimal "results", :precision => 8, :scale => 4, :default => 0.0
thanks!
You can do this way
change_column :table_name, :column_name, default: 0.0
It will remove the precision and scale from the corresponding column.
I am new to Ruby on Rails. I have a migration called create user
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.column :username, :string, :limit => 25, :default => "", :null => false
t.column :hashed_password, :string, :limit => 40, :default => "", :null => false
t.column :first_name, :string, :limit => 25, :default => "", :null => false
t.column :last_name, :string, :limit => 40, :default => "", :null => false
t.column :email, :string, :limit => 50, :default => "", :null => false
t.column :display_name, :string, :limit => 25, :default => "", :null => false
t.column :user_level, :integer, :limit => 3, :default => 0, :null => false
end
User.create(:username=>'test',:hashed_password=>'test',:first_name=>'test',:last_name=>'test',:email=>'test#test.com',:display_name=> 'test',:user_level=>9)
end
end
When I run rake db:migrate the table is created with the columns as mentioned above but the test data are not there
mysql>select * from users;
Empty set (0.00 sec)
EDIT I just dropped the whole database and restarted the migration and now it is showing the following error.
rake aborted!
An error has occurred, all later migrations canceled:
Can't mass-assign protected attributes: username, hashed_password, first_name, last_name, email, display_name, user_level
What am I doing wrong please help?
Thank you.
add
attr_accessible :username, :hashed_password, :first_name, :last_name, :email, :display_name, :user_level
to your user.rb
That's Rails way of prohibiting users to create or update objects via a param hash. You need to specify your User attributes as attr_accessible in your model:
example :
class User
attr_accessible :username, :firstname (etc)
end
Read more about Mass Assignment here.
just to complete the answer about testing environment. You can run rake db:test:prepare to check the migrations and load schema !
I wanted to add two columns to the migration that makes the notifications for the mailboxer gem. When I place both in the migration that creates the notification and then run a migration it goes through. I then input them into the form and when I submit them there are no errors and the log shows that its take them. For some reason when I try to display them they don't show up, the only thing that is showing is the original things that were part of the migration which is subject and body. My question is how can I add columns to this migration?
Here is the migration with my two columns of lat and long added to the notifications section.
# This migration comes from mailboxer_engine (originally 20110511145103)
class CreateMailboxer < ActiveRecord::Migration
def self.up
#Tables
#Conversations
create_table :conversations do |t|
t.column :subject, :string, :default => ""
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Receipts
create_table :receipts do |t|
t.references :receiver, :polymorphic => true
t.column :notification_id, :integer, :null => false
t.column :read, :boolean, :default => false
t.column :trashed, :boolean, :default => false
t.column :deleted, :boolean, :default => false
t.column :mailbox_type, :string, :limit => 25
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Notifications and Messages
create_table :notifications do |t|
t.column :type, :string
t.column :body, :text
t.column :subject, :string, :default => ""
t.column :lat, :text
t.column :long, :text
t.references :sender, :polymorphic => true
t.references :object, :polymorphic => true
t.column :conversation_id, :integer
t.column :draft, :boolean, :default => false
t.column :updated_at, :datetime, :null => false
t.column :created_at, :datetime, :null => false
end
#Indexes
#Conversations
#Receipts
add_index "receipts","notification_id"
#Messages
add_index "notifications","conversation_id"
#Foreign keys
#Conversations
#Receipts
add_foreign_key "receipts", "notifications", :name => "receipts_on_notification_id"
#Messages
add_foreign_key "notifications", "conversations", :name => "notifications_on_conversation_id"
end
def self.down
#Tables
remove_foreign_key "receipts", :name => "receipts_on_notification_id"
remove_foreign_key "notifications", :name => "notifications_on_conversation_id"
#Indexes
drop_table :receipts
drop_table :conversations
drop_table :notifications
end
end
My show view looks like
%h1= conversation.subject
%ul
= content_tag_for(:li, conversation.receipts_for(current_user)) do |receipt|
- message = receipt.message
%h3= message.subject
%p= message.body
%p= message.lat
%p= message.long
= render 'messages/form', conversation: conversation
This is what comes up in the console, for lat and long you see it says null
Notification Load (0.1ms) SELECT "notifications".* FROM "notifications" ORDER BY "notifications"."id" DESC LIMIT 1
--- !ruby/object:Message
attributes:
id: 4
type: Message
body: game
subject: wiz
lat: !!null
long: !!null
sender_id: 1
sender_type: User
conversation_id: 2
draft: false
updated_at: 2013-03-10 04:37:54.984277000Z
created_at: 2013-03-10 04:37:54.984277000Z
notified_object_id: !!null
notified_object_type: !!null
notification_code: !!null
attachment: !!null
=> nil
I am not sure I understand exactly what you are doing, but it sounds like possibly you haven't added the two new columns to attr_accessible and they aren't being saved because of that. That's the first thing I would check (it's in the model).
Otherwise, go to the console and see if your new columns are there and see if there is data in them. That will help you find where the problem is.
I'm using Rails 3.0.3 (don't ask) and when I run a migration for table with decimal column and set :default => 0 it's re-setting the column's scale and precision to (10,0).
def self.up
create_table :courses do |t|
t.integer :user_id
t.string :name
t.decimal :distance, :precision => 5, :scale => 2, :default => 0
t.text :notes
t.timestamps
end
end
When I remove the :default=>0 option from the migration the column's scaled and precision are correct: (5,2)
I tried running a change_column migration with only :default =>: 0 set, but the column's scale and precision were re-set to (10,0)
change_column :courses, :distance, :decimal, :default => 0.0
I know I can go into MySQL and correct the precision and scale of the column, but wondering if I'm doing something wrong or if this is a bug?
Google reveals no information so I think I'm doing something wrong.
Try this one: t.decimal :distance, :precision => 5, :scale => 2, :default => 0.00
I was also stuck on this one, and i cant find a solution to it. Eventually i had to go into mysql and change the required precision, scale and default value, i used this from here with a few modifications
mysql> ALTER TABLE question ADD (price INTEGER);
mysql> ALTER TABLE question DROP price;
mysql> ALTER TABLE question ADD (frig DECIMAL(5,2));
mysql> ALTER TABLE question CHANGE frig price DECIMAL(5,2);
mysql> ALTER TABLE question ALTER status SET DEFAULT '0';
mysql> ALTER TABLE question MODIFY price INTEGER;
Also try :default => 0.0 #note the 0.0 as the default value must be in the data type specified i.e. decimal
Hope it helps.
You can also do
def change
change_column :courses , :distance, :decimal, :precision => 5, :scale => 2, :null => false, :default => '0'
end
I am just trying this right now and seems to work.
Active Records Migrations
class ChangeVisitratioFormatInCampaigns < ActiveRecord::Migration[5.0]
def change
reversible do |dir|
change_table :campaigns do |t|
dir.up { t.change :visitratio, :decimal, :precision => 5, :scale => 4, :default => 1 }
dir.down { t.change :visitratio, :integer }
end
end
end
end
Current migration:
t.string "email", :default => "", :null => false
add_index :users, :email, :unique => true
I want to create a new migration to remove the :null => false requirement and also remove the default => "" for email. Also, I would like to change the index to remove :unique => true. What's the syntax?
I haven't done much with indices, and there doesn't seem to be a change_index method on ActiveRecord::Migration, but you can try something like this:
class ChangeUserStuff < ActiveRecord::Migration
def self.up
change_column :users, :email, :default => "", :null => true
remove_index :users, :column => :email
add_index :users, :email
end
def self.down
change_column :users, :email, :default => "", :null => false
remove_index :users, :column => :email
add_index :users, :email, :unique => true
end
end
There was some funny behavior regarding changing :null options, but I believe setting them to true instead of omitting should handle it.