0

I have a parent model that has many child models

I want to destroy duplicate child records based on one parameter of the child.

I tried this:

 parent.child.uniq! {|child| child.parameter }

It apparently works but the database does not reflects the change.

rb512
  • 6,880
  • 3
  • 36
  • 55
user2376068
  • 207
  • 1
  • 4
  • 11
  • Did you try saving the record to the database after changing it? – lurker Oct 19 '13 at 01:36
  • mbratch,how would you do that? I tried parent.save and parent.child.save but neither of those worked. – user2376068 Oct 19 '13 at 02:04
  • 1
    My question back is, if you're using a database and running into duplicates in a field that should be unique, why don't you have a unique index on it that enforces that constraint? Let the DBM do its job, and rely on it to do it. Rails is designed for that sort of behavior and makes it easy to design in. – the Tin Man Oct 19 '13 at 04:43

1 Answers1

1

Your operation is performed on an object retrieved from database and not the actual database. Check out this post for the right answer.

Community
  • 1
  • 1
rb512
  • 6,880
  • 3
  • 36
  • 55