diff --git a/Tutorial_Docs/Associations.MD b/Tutorial_Docs/Associations.MD index 7ebbd8a..caa453e 100644 --- a/Tutorial_Docs/Associations.MD +++ b/Tutorial_Docs/Associations.MD @@ -45,6 +45,13 @@ You would then access the methods added to the model in rails console via pages.editors ``` +### foreign_key +If your foreign key is something other than the associated class_name, you must specify a different foreign key +``` +belongs_to :editor, :class_name => "AdminUser", :foreign_key => "admin_user_id" # because foreign key is not called editor_id + +``` + ## Defining editors on pages In rails console, create a new user ``` @@ -60,3 +67,18 @@ Associate the new user to the page ``` page.editors << me ``` + + +## Rich Many-to-many +Create new model for rich join table +``` +rails generate model SectionEdit +``` + +Adding join between tables +``` +edit = SectionEdit.new +``` +``` +edit.summary = "Edited some content" +``` diff --git a/app/models/admin_user.rb b/app/models/admin_user.rb index f5ecb98..890a856 100644 --- a/app/models/admin_user.rb +++ b/app/models/admin_user.rb @@ -1,6 +1,7 @@ class AdminUser < ApplicationRecord has_and_belongs_to_many :pages + has_many :section_edits # To configure a different table name. # self.table_name = "admin_users" diff --git a/app/models/section.rb b/app/models/section.rb index ec21dc1..b88d176 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -1,2 +1,4 @@ class Section < ApplicationRecord + has_many :section_edits + end diff --git a/app/models/section_edit.rb b/app/models/section_edit.rb new file mode 100644 index 0000000..5d3dcdd --- /dev/null +++ b/app/models/section_edit.rb @@ -0,0 +1,5 @@ +class SectionEdit < ApplicationRecord + + belongs_to :editor, :class_name => "AdminUser", :foreign_key => "admin_user_id" # because foreign key is not called editor_id + belongs_to :section +end diff --git a/db/migrate/20160927050319_create_section_edits.rb b/db/migrate/20160927050319_create_section_edits.rb new file mode 100644 index 0000000..8f46de3 --- /dev/null +++ b/db/migrate/20160927050319_create_section_edits.rb @@ -0,0 +1,12 @@ +class CreateSectionEdits < ActiveRecord::Migration[5.0] + + def change + create_table :section_edits do |t| # rich many-to-many does require primary key + t.references :admin_user + t.references :section + t.string :summary + t.timestamps + end + end + +end diff --git a/test/fixtures/section_edits.yml b/test/fixtures/section_edits.yml new file mode 100644 index 0000000..80aed36 --- /dev/null +++ b/test/fixtures/section_edits.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/section_edit_test.rb b/test/models/section_edit_test.rb new file mode 100644 index 0000000..b1c59d8 --- /dev/null +++ b/test/models/section_edit_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SectionEditTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end