Iker Narvaez

fix group structure

class Group < ApplicationRecord
belongs_to :user, -> { with_deleted }, inverse_of: :groups
belongs_to :pool, -> { with_deleted }, inverse_of: :groups
has_and_belongs_to_many :users, inverse_of: :groups
validates :name, :pool, presence: true
end
......
......@@ -17,7 +17,6 @@ class Pool < ApplicationRecord
has_many :matches, dependent: :destroy, inverse_of: :pool
has_many :groups, inverse_of: :pool
has_many :users, through: :groups, inverse_of: :pools
scope :active, -> { where(active: true) }
accepts_nested_attributes_for :matches, reject_if: :all_blank,
......
......@@ -27,8 +27,7 @@ class User < ApplicationRecord
before_save :encrypt_password
has_many :bets, inverse_of: :user
has_many :groups, inverse_of: :user
has_many :pools, through: :groups, inverse_of: :users
has_and_belongs_to_many :groups, inverse_of: :users
has_and_belongs_to_many :roles, inverse_of: :users,
class_name: 'HeimdallEngine::Role',
......
......@@ -10,5 +10,6 @@ Rails.application.routes.draw do
namespace :admin do
resources :pools
resources :groups
end
end
......
class CreateGroups < ActiveRecord::Migration[5.0]
def change
create_table :groups do |t|
t.integer :user_id, index: true
t.integer :pool_id, index: true
t.string :name
t.timestamps
end
end
......
class CreateGroupUser < ActiveRecord::Migration[5.0]
def change
create_table :group_users do |t|
t.integer :group_id, index: true
t.integer :user_id, index: true
end
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180611152223) do
ActiveRecord::Schema.define(version: 20180611152633) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -24,13 +24,19 @@ ActiveRecord::Schema.define(version: 20180611152223) do
t.integer "points"
end
create_table "groups", force: :cascade do |t|
create_table "group_users", force: :cascade do |t|
t.integer "group_id"
t.integer "user_id"
t.index ["group_id"], name: "index_group_users_on_group_id", using: :btree
t.index ["user_id"], name: "index_group_users_on_user_id", using: :btree
end
create_table "groups", force: :cascade do |t|
t.integer "pool_id"
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["pool_id"], name: "index_groups_on_pool_id", using: :btree
t.index ["user_id"], name: "index_groups_on_user_id", using: :btree
end
create_table "heimdall_engine_role_abilities", force: :cascade do |t|
......