I have two tables talks and media.
- The
talksmay have 0 or 1media. - The
mediacan have other rows that is not associated to anytalks.
I'm planning to add a foreign key to the talks table to a field media_id (that references media), so that if media is deleted, media_id would be null. Though not super necessary, it'd be great to have the associated media to be deleted when talks row is deleted, if any.
The current migration looks like, it runs fine and generates the media_id on the talks table as I want:
class AddMediaToTalks < ActiveRecord::Migration[6.0]
def change
add_reference :talks, :media, type: :integer, foreign_key: true
end
end
But, I'm not so sure about what I need to put on the models, I mean has_one and belongs_to. I am not sure which would go to which model, or even if I need only one or neither or both.
I also tried adding on_delete: :nullify to the migration script, but it doesn't really have any effect. I'm using mysql, btw. When I try to delete associated media, it fails because it is referenced in the talks table.
Disclaimer : I am fairly new to Rails. Thanks.