Rails Admin and dealing with Calculated Totals,Subtotals and Discount fields - ruby-on-rails

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.

Related

Association problems in between models RAILS 4

My models
CAR BRANDS MODEL
class CarBrand < ActiveRecord::Base
has_many :car_ads
end
CAR ADVERTISEMENTS MODEL
class CarAd < ActiveRecord::Base
has_one :car_brand
end
my controller:
def index
#car_ads = CarAd.all.order("car_ads.created_at DESC")
end
car ads migrations:
class CreateCarAds < ActiveRecord::Migration
def up
create_table :car_ads do |t|
t.integer "user_id"
t.integer "car_brand_id"
t.integer "car_model_id"
t.integer "state_id", :limit => 2
t.integer "vin_id"
t.integer "year_manufac", :precision => 4
t.integer "km_age"
t.integer "price_usd", :limit => 7
t.integer "car_tel_number", :precision => 8
t.float "motor_volume", :limit => 10
t.string "transmission"
t.integer "horse_power", :limit => 3
t.text "description"
t.boolean "visible", :default => true
t.boolean "active", :default => true
t.string "photo_file_name"
t.string "photo_content_type"
t.integer "photo_file_size"
t.datetime "photo_updated_at"
t.timestamps null: false
end
add_index :car_ads, :user_id
add_index :car_ads, :car_brand_id
add_index :car_ads, :car_model_id
add_index :car_ads, :state_id
add_index :car_ads, :vin_id
end
def down
drop_table :car_ads
end
end
Car brands migratiions
class CreateCarBrands < ActiveRecord::Migration
def up
create_table :car_brands do |t|
t.string "brand", :limit => 20
t.timestamps null: false
end
end
def down
drop_table :car_brands
end
end
so the problem is that i cant get car brand form car ads, please help,
i wanted to get that like
iterating
<% #car_ads.each do |carad|%>
<%= carad.car_brand %>
<%end%>
Modify CAR ADVERTISEMENTS MODEL
class CarAd < ActiveRecord::Base
belongs_to :car_brand
end
Modify your controller:
def index
#car_ads = CarAd.all.order("created_at DESC")
end
You didn't add any reference to CarBrand in CarAd table, just add the car_ad_id column with a migration like this
class AddCarBradIdToCarAd < ActiveRecord::Migration
def change
add_column :car_ads, :car_brand_id, :integer
end
end
So rails would be able to get the corresponding CarBrand from a CarAd

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 comment out migration file for development?

I'm running this migration file on local and running into problems because part is MySQL which does not work because I'm using Sqlite3. I want to make sure the SQL runs only in production not in development. I understand I need to wrap part of this with this comment so that I can successfully run this migration:
unless Rails.env == "development".
How can I add this to the script below?
class Places < ActiveRecord::Migration
def self.up
create_table :countries do |t|
t.string :name, :limit => 50, :null => false
t.string :fips104, :limit => 2, :null => false
t.string :iso2, :limit => 2, :null => false
t.string :iso3, :limit => 3, :null => false
t.string :ison, :limit => 4, :null => false
t.string :internet, :limit => 2, :null => false
t.string :capital, :limit => 25
t.string :map_reference, :limit => 50
t.string :nationality_singular, :limit => 35
t.string :nationaiity_plural, :limit => 35
t.string :currency, :limit => 30
t.string :currency_code, :limit => 3
t.integer :population
t.string :title, :limit => 50
t.string :comment, :limit => 255
t.timestamps
end
create_table :regions do |t|
t.references :country, :null => false
t.string :name, :limit => 45, :null => false
t.string :code, :limit => 8, :null => false
t.string :adm1code, :limit => 4, :null => false
t.timestamps
end
create_table :cities do |t|
t.references :country, :null => false
t.references :region, :null => false
t.string :name, :limit => 45, :null => false
t.float :latitude, :null => false
t.float :longitude, :null => false
t.string :timezone, :limit => 10, :null => false
t.integer :dma_id
t.string :county, :limit => 25
t.string :code, :limit => 4
t.timestamps
end
add_index :cities, :name
execute "LOAD DATA INFILE '#{RAILS_ROOT}/db/migrate/Countries.txt' INTO TABLE countries
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;"
execute "LOAD DATA INFILE '#{RAILS_ROOT}/db/migrate/Regions.txt' INTO TABLE regions
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;"
execute "LOAD DATA INFILE '#{RAILS_ROOT}/db/migrate/Cities.txt' INTO TABLE cities
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;"
end
def self.down
drop_table :countries
drop_table :regions
drop_table :cities
end
end
In your self.up method you can check environment like below:
if Rails.env.production?
#your mysql code which runs only in production env
end

How do I write the opposite of this migration?

My migration right now :
class CreateActivities < ActiveRecord::Migration
def self.up
create_table :activities do |t|
t.integer :account_id, :null => false
t.integer :target_id, :null => false
t.string :target_type, :null => false
t.string :event_type, :null => false
t.integer :employee_id
t.string :name
t.timestamps
end
add_index :activities, [:target_id, :target_type]
With this I can now call a target like so :
Activity.first.target
And it will bring up the target_id, based on the target_type.
How would I do the opposite of that so that I can select a target, and if it has any associated Activities, they will show up?
Like so :
Job.find(1234).activities
# Where Job.find(1234) is the target_id of many activities.
If I understand right, this should do it:
class Job < ActiveRecord::Base
has_many :activities, :conditions => ['target_type = ?', 'Job'], :as => :target
end

Can't access related table

I am really under pressure and totally new to Rails. Project due in two days.
I have done a search on Rails console for a user inputted in my database user = user.find(1) and it returns.
the user has_many recipes
recipes belongs to user ....defined in user and recipe models respectively.
I am getting the error.
undefined method "recipe" for user?
This is how I created my User
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.integer "position"
t.boolean "visible", :default => false
t.string "restaurant_name", :limit => 25
t.string "username", :limit => 25
t.string "password", :limit => 50
t.string "hashed_email", :default => "", :null => false
t.string "address1", :limit => 80
t.string "address2", :limit => 80
t.string "address3", :limit => 80
t.string "county", :limit => 40
t.string "telephone", :limit => 40
t.text "web address", :limit => 60
t.string "photo_path", :limit => 100
t.text "description", :limit => 2000
t.text "salt", :string, :limit => 50
t.timestamps
end
end
def self.down
drop_table :users
end
end
Recipe
class CreateRecipes < ActiveRecord::Migration
def self.up
create_table :recipes do |t|
t.integer "user_id"
t.string "recipe_name", :limit => 100
t.integer "greedy_type", :default => 0
t.string "photo_path", :limit => 40
t.text "description", :limit => 400
t.text "recipe", :limit => 4000
t.integer "ingredient1", :limit => 40
t.integer "ingredient2", :limit => 40
t.integer "ingredient3", :limit => 40
t.integer "ingredient4", :limit => 40
t.string "permalink"
t.integer "position"
t.boolean "visible", :default => false
t.timestamps
end
add_index("recipes", "user_id")
add_index("recipes", "permalink")
end
def self.down
drop_table :recipes
end
end
users.rb
class Users < ActiveRecord::Base
has_many :recipes
end
recipe.rb
class Recipes < ActiveRecord::Base
belongs_to :users
has_many :restaurants
end
ERROR
irb(main):005:0> users.recipe
NoMethodError: undefined method `recipe' for #<Users:0x599ebc0>
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activemodel-3.0.9/lib/active_model/attribute_methods.rb:
392:in `method_missing'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/active_record/attribute_methods.r
b:46:in `method_missing'
If User has_many recipes, why you add field recipe_id for user? You should add user_id field to the Recipe class table. After creating recipe just store user_id and that is all you need. Cheers!
as user has many recipes,
you have to write something like to get first users recipe..
recipe = user.recipes.first
and store user_id in recipe table.

Resources