Kevin Whitaker
Kevin Whitaker

Reputation: 13425

Rails - delete one side of a one-to-many relationship

Quick question:

If I have a one-to-many relationship, such as this:

class SalesPerson < ActiveRecord::Base
  has_many :deals
end

class Deal < ActiveRecord::Base
  belongs_to :sales_person
end

how can I delete a Sales Person, without negatively impacting the deals associated with them? The use-case for this would be if someone left the organization. We still need a record of the deals in the database, but that sales person record is no longer needed.

Would it just be better to have an active/inactive flag on the sales person instead?

Thanks.

Upvotes: 1

Views: 429

Answers (1)

jpemberthy
jpemberthy

Reputation: 7533

For this kind of situations I've use acts_as_paranoid, basically It adds a new timestamps column: deleted_at and overrides some of your AR finders.

Upvotes: 1

Related Questions