add homie to the gang when homie is created

This commit is contained in:
Bilal
2020-10-07 22:47:19 +03:00
parent 8d7716f2e0
commit 8832ba071b
9 changed files with 43 additions and 5 deletions

View File

@@ -1,4 +1,10 @@
class GangsController < ApplicationController class GangsController < ApplicationController
def index
json_response Gang.all
rescue StandardError
error_response :bad_request
end
def show def show
if Gang.count.zero? if Gang.count.zero?
Gang.create.save Gang.create.save
@@ -19,7 +25,8 @@ class GangsController < ApplicationController
private private
def gang_params def gang_params
params.require(:gang).permit :chip_name, params.require(:gang).permit :name,
:chip_name,
:chip_code, :chip_code,
:chip_symbol, :chip_symbol,
:chip_scale, :chip_scale,

View File

@@ -46,7 +46,8 @@ class HomiesController < ApplicationController
params.require(:homie).permit( params.require(:homie).permit(
:name, :name,
:importance, :importance,
:about :about,
:gang_id
) )
end end
end end

View File

@@ -1,3 +1,3 @@
class Gang < ApplicationRecord class Gang < ApplicationRecord
has_many :homies
end end

View File

@@ -2,6 +2,8 @@ class Homie < ApplicationRecord
has_many :money_moves has_many :money_moves
has_many :work has_many :work
belongs_to :gang
def self.info(importance) def self.info(importance)
cash_totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount) cash_totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount)
work_totals = Homie.all.joins(:work).group(:id).order(:id).sum(:amount) work_totals = Homie.all.joins(:work).group(:id).order(:id).sum(:amount)

View File

@@ -1,7 +1,7 @@
Rails.application.routes.draw do Rails.application.routes.draw do
constraints format: :json do constraints format: :json do
scope :api do scope :api do
resources :gangs, only: [:show, :update] resources :gangs, only: [:index, :show, :update]
resources :money_moves resources :money_moves
resources :work resources :work
resources :chips, only: %i[index create destroy] resources :chips, only: %i[index create destroy]

View File

@@ -0,0 +1,5 @@
class AddNameToGangs < ActiveRecord::Migration[5.2]
def change
add_column :gangs, :name, :text
end
end

View File

@@ -0,0 +1,5 @@
class AddGangRelationToHomies < ActiveRecord::Migration[5.2]
def change
add_reference :homies, :gang
end
end

View File

@@ -0,0 +1,15 @@
class AddInitialGangAndConnectToHomies < ActiveRecord::Migration[5.2]
def up
# execute "INSERT INTO gangs SELECT 'name' WHERE NOT EXISTS (SELECT * FROM gangs)"
if Gang.count.zero?
Gang.create.save
end
gang = Gang.first
Homie.all.update(gang: gang)
end
def down
Homie.all.update(gang: nil)
end
end

View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_10_07_180902) do ActiveRecord::Schema.define(version: 2020_10_07_213411) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -43,6 +43,7 @@ ActiveRecord::Schema.define(version: 2020_10_07_180902) do
t.integer "chip_scale" t.integer "chip_scale"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.text "name"
end end
create_table "homies", force: :cascade do |t| create_table "homies", force: :cascade do |t|
@@ -51,6 +52,8 @@ ActiveRecord::Schema.define(version: 2020_10_07_180902) do
t.text "about" t.text "about"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.bigint "gang_id"
t.index ["gang_id"], name: "index_homies_on_gang_id"
end end
create_table "money_moves", force: :cascade do |t| create_table "money_moves", force: :cascade do |t|