-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We would like groups to have zero or more forms. Ideally we would do this without adding data to the form record in the forms-api database about data in the forms-admin database. This leads us to using a join table, so we can associate groups with forms but also do the reverse lookup. Normally to use a join table in Rails we use the `has_and_belongs_to_many` association [[1]]. However, models in ActiveResource cannot be associated with models in ActiveRecord [[2]]. This means we can't use any of the usual methods to maintain the association. Instead, this commit takes the approach of having an explicit join model. It's clunky, but is the best approach I've tried so far. It doesn't rely too much on understanding Rails magic, and it doesn't require writing any SQL. Probably the proper fix for this issue is keeping the form records and the group records in the same database, but that is outside the scope of this ticket. [1]: https://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association [2]: rails/activeresource#292
- Loading branch information
Showing
5 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class GroupForm < ApplicationRecord | ||
self.primary_key = %i[form_id group_id] | ||
self.table_name = :groups_form_ids | ||
|
||
belongs_to :group | ||
|
||
def form | ||
Form.find(form_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class CreateJoinTableFormGroup < ActiveRecord::Migration[7.1] | ||
def change | ||
create_join_table :forms, :groups, table_name: :groups_form_ids do |t| | ||
t.index :form_id, unique: true | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters