ruby rails remove scale and precision from database - ruby-on-rails

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.

Related

Rails Admin and dealing with Calculated Totals,Subtotals and Discount fields

Ok so I have a table orders, order_items, and items. Everything works fine except for 1 thing.
class CreateOrders < ActiveRecord::Migration[5.0]
def change
create_table :orders do |t|
t.integer :seller_id
t.integer :customer_id
t.string :shipping_method
t.string :shipping_terms
t.string :payment_method
t.date :delivery_date
t.date :due_date
t.decimal :total_discount, :precision =>8, :scale => 2
t.decimal :subtotal, :precision =>8, :scale => 2
t.decimal :sales_tax, :precision =>8, :scale => 2
t.decimal :total, :precision =>8, :scale => 2
t.timestamps
end
end
end
class CreateOrderItems < ActiveRecord::Migration[5.0]
def change
create_table :order_items do |t|
t.integer :order_id
t.integer :item_id
t.integer :quantity
t.decimal :discount, :precision =>8, :scale => 2
t.decimal :line_total, :precision =>8, :scale => 2
t.boolean :packed
t.string :packed_by
t.string :ffr
t.timestamps
end
end
end
class CreateItems < ActiveRecord::Migration[5.0]
def change
create_table :items do |t|
t.integer :sku
t.string :name
t.decimal :price, :precision =>8, :scale => 2
t.string :description
t.decimal :weight, :precision =>8, :scale => 2
t.timestamps
end
end
end
Basically there needs to be calculations done either as the items are being added to the order or before everything is saved.
On the Orders table the following needs to happen
Calculate : total_discount(dollar amount), sub_total, :sales_tax, :total
On OrderItems table the following needs to happen
Calculate : line_total, discount(dollar amount)
There has to be a sane way to do this but I have yet to figure it out. Either I really do not know what I am looking for or I am googling all the wrong things.

Rails change_column migration

I have this inside create_table:
t.string 'email', :default => '', :null => false
And then in another migration have this:
change_column('admin_users', 'email', :string, :limit => 100)
After I run everything, in schema.rb I get this:
t.string "email", limit: 100, default: "", null: false
Wasn't change_column supposed to overwrite everything in the previous definition? Why did :default and null were left? I was watching a tutorial where it said change_column overwrites everything. Was there some recent Rails version when this was changed?
Not necessarily, change_column does not erase previously-set details. Let's say you were changing the column so you could simply add a NULL constraint. It wouldn't make sense to have to add in all the other previously-set contraints as well.
If you want to change the default or null settings, just do so in the change_column method.
change_column('admin_users', 'email', :string, :limit => 100, :default => "", :null => true)
Otherwise, to erase everything, do remove_column then add_column:
remove_column('admin_users', 'email')
add_column('admin_users', 'email', :string, :limit => 100)
change_column doesn't overwrite everything, it just makes the changes you specify. So it added the limit to the column, but that's all.

Creating a table per account

I have an SMS gateway with a central price table referenced by all accounts. However, there is a requirement for unique prices for different routes for accounts.
I cannot serve this from the same price table. I was thinking of having a prices table for every account which is created automatically on account creation and specific pricing can be defined for specific account.
Any idea how i can implement this in Ruby on Rails? How can i make sweeping changes accross all tables?
The schema for table looks like this;
create_table "prices", :force => true do |t|
t.string "country"
t.string "network"
t.decimal "price_euros", :precision => 10, :scale => 3
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.decimal "price_ugx", :precision => 10, :scale => 3
t.decimal "price_kes", :precision => 10, :scale => 3
t.decimal "price_tzs", :precision => 10, :scale => 3
t.decimal "price_usd", :precision => 10, :scale => 3
end
Thank you

Rails Migration Decimal Column :default=> 0 re-setting MySQL precision to 0?

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

Rails 3 Migration Alter Float Length/Decimal

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?

Resources