Skip to content

Commit

Permalink
Rich many-to-many join table
Browse files Browse the repository at this point in the history
  • Loading branch information
kinseyost committed Sep 27, 2016
1 parent 5e8c667 commit ed7433f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Tutorial_Docs/Associations.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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"
```
1 change: 1 addition & 0 deletions app/models/admin_user.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions app/models/section.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
class Section < ApplicationRecord
has_many :section_edits

end
5 changes: 5 additions & 0 deletions app/models/section_edit.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions db/migrate/20160927050319_create_section_edits.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions test/fixtures/section_edits.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/models/section_edit_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class SectionEditTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit ed7433f

Please sign in to comment.