user3464216
user3464216

Reputation:

ActiveRecord associations

I have a database compose of Scenes and Characters

A character belongs_to zero or many scenes and a scene can have zero or many characters.

Scene model

class Scene < ActiveRecord::Base
    belongs_to :chapter
    has_many :characters
end

Scene database

class CreateScenes < ActiveRecord::Migration
  def change
    create_table :scenes do |t|
      t.text :narrative
      t.string :place
      t.string :period
      t.integer :chapter_id

      t.timestamps
    end
  end
end

Character model

class Character < ActiveRecord::Base
    belongs_to :scene
end

Character database

class CreateCharacters < ActiveRecord::Migration
  def change
    create_table :characters do |t|
      t.string :name
      t.string :role
      t.integer :scene_id

      t.timestamps
    end
  end
end

When I delete a scene, I want to update the character and turn to blank the column scene_id. I used dependent: :destroy, but it does not do what I'm looking for.

I hope you understant what I want, and sorry for my bad english Thanks

Upvotes: 2

Views: 44

Answers (1)

j-dexx
j-dexx

Reputation: 10416

Try dependent: :nullify which will set the column to nil. I think that's what you're looking for

Upvotes: 2

Related Questions