diff --git a/Tutorial_Docs/Associations.MD b/Tutorial_Docs/Associations.MD index 9617baa..7ebbd8a 100644 --- a/Tutorial_Docs/Associations.MD +++ b/Tutorial_Docs/Associations.MD @@ -24,3 +24,39 @@ subject.pages.size (When join table only has foreign keys) * has_and_belongs_to_many (HABTM method) * has_and_belongs_to_many + + +## Naming associations +### join_table +When working with Many to many, the name of the join table should be alphabetical, and plural. +If you don't name the table according to this standard, you can specify the `join_table` name like this. +``` +has_and_belongs_to_many :admin_users, :join_table => "admin_users_pages" +``` +Note, this has to be done in admin_users also. + +### class_name +If you have a more applicable association name based on the association type, you can rename the association type by defining the association with that name and telling the model the `class_name` +``` +has_and_belongs_to_many :editors, :class_name => "AdminUser" +``` +You would then access the methods added to the model in rails console via +``` +pages.editors +``` + +## Defining editors on pages +In rails console, create a new user +``` +me = AdminUser.create(:first_name => "Kinsey", :last_name => "Van Ost", :username => "kinseyost") +``` + +Find the page you want to add a user to +``` +page = Page.find(2) +``` + +Associate the new user to the page +``` +page.editors << me +``` diff --git a/app/models/admin_user.rb b/app/models/admin_user.rb index f0ef1a1..f5ecb98 100644 --- a/app/models/admin_user.rb +++ b/app/models/admin_user.rb @@ -1,7 +1,9 @@ class AdminUser < ApplicationRecord - + + has_and_belongs_to_many :pages + # To configure a different table name. # self.table_name = "admin_users" - + # ApplicationRecord already knows about the table because it is inheriting from ActiveRecord::Base end diff --git a/app/models/page.rb b/app/models/page.rb index 176ee1d..2e58865 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -1,5 +1,9 @@ class Page < ApplicationRecord belongs_to :subject + # specify the correct table using class_name (with capital case) + has_and_belongs_to_many :editors, :class_name => "AdminUser" + # If join table has different name, you specify (in both sides of the relationship) like this: + # has_and_belongs_to_many :admin_users, :join_table => "admin_users_pages" end diff --git a/db/migrate/20160927043409_create_admin_users_pages_join.rb b/db/migrate/20160927043409_create_admin_users_pages_join.rb new file mode 100644 index 0000000..d663dac --- /dev/null +++ b/db/migrate/20160927043409_create_admin_users_pages_join.rb @@ -0,0 +1,15 @@ +class CreateAdminUsersPagesJoin < ActiveRecord::Migration[5.0] + + def up + create_table :admin_users_pages, :id => false do |t| # Remove the primary key **Very important + t.integer "admin_user_id" + t.integer "page_id" + end + add_index :admin_users_pages, ["admin_user_id", "page_id"] + end + + def down + drop_table :admin_users_pages + end + +end diff --git a/db/schema.rb b/db/schema.rb index b421145..e6cb0f9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160920033121) do +ActiveRecord::Schema.define(version: 20160927043409) do create_table "admin_users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| t.string "first_name", limit: 25 @@ -23,6 +23,12 @@ t.index ["username"], name: "index_admin_users_on_username", using: :btree end + create_table "admin_users_pages", id: false, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| + t.integer "admin_user_id" + t.integer "page_id" + t.index ["admin_user_id", "page_id"], name: "index_admin_users_pages_on_admin_user_id_and_page_id", using: :btree + end + create_table "pages", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| t.integer "subject_id" t.string "name"