rails, deleting record if exists with a same column value before creating a new record
rails-activerecord, ruby-on-rails, ruby-on-rails-3
Solution
your logic is complicated enough to not be built into a framework, you can do what you need in either controller or model, just find the photo you want to replace, destroy it if it exists and create new one
photo = person.photos.where(kind: "face").first
photo.destroy if photo
# procede with creating new photo object
Problem
i have an object person which has a "has_many" relationship with photo object ``` has_many :photos, :foreign_key => 'person_id', :dependent => :destroy ``` and on photo: ``` belongs_to :person, :foreign_key => 'person_id' ``` what i want to do. is during an update of person, if a user uploaded a new photo to that person with the same "kind" (which is an attribute of photo) so the old kind be deleted from the db for example: an update is being performed on person with an uploaded photo with kind "face" if a record of photo already exists with the same 'person_id' and the same "kind" it will first be deleted (not updated) and only the then the new record be saved thanks