I have a self-referential relationship set up in my app so that an AppForm
can belong_to
another AppForm
through Variations
.
The behavior I am trying to create is a button on the AppForm
's show
page that can be clicked to create a new variation of the AppForm
. This button would ideally create a duplicate of the current record and take the user to that new record's edit
page to make changes before saving it.
I have the variations
table set up like this in my schema.rb
:
create_table "variations", force: :cascade do |t|
t.bigint "app_form_id"
t.bigint "original_app_form_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id"
t.index ["app_form_id", "original_app_form_id"], name: "index_variations_on_app_form_id_and_original_app_form_id", unique: true
t.index ["app_form_id"], name: "index_variations_on_app_form_id"
t.index ["original_app_form_id"], name: "index_variations_on_original_app_form_id"
t.index ["user_id"], name: "index_variations_on_user_id"
end
With this as my variation.rb
model:
class Variation < ApplicationRecord
belongs_to :user
belongs_to :app_form
belongs_to :original_app_form, class_name: "AppForm"
end
And this in my app_form.rb
model:
class AppForm < ApplicationRecord
belongs_to :application_status, optional: true
belongs_to :final_decision, optional: true
belongs_to :funding_status, optional: true
belongs_to :user, optional: true
belongs_to :app_form, through: :variations
end
This seems pretty standard for a has_many :through
relationship, but now I can't seem to Google my way to creating the duplicate variation of an AppForm
. I've seen posts like this one that show a variety of different ways to do this. Some use routes, others need special gems...I'm lost. What's the "Rails-y" way to do this properly?