Dimitri Dimitri - 5 months ago 12
Ruby Question

How delete relation belongs_to - has_one between objects

I have two models:

Project
with
has_one :performer
and
Performer
with
belongs_to :project


I have object project with relationship with performer like
project.performer


How can I delete this relationship? I need the attribute
performer_id
to have value
nil
.

Answer

You just update your Project object.

class Project < ActiveRecord::Base
  ...
  def remove_performer
    update(performer_id: nil)
  end
  ...
end

Add method remove_performer , then you can use it to remove performer from project.

proj = Project.new
proj.remove_performer