Solved!
The problem was that, because is_primary
is a boolean, its false values have to be unique as well, not only true ones. So when inserting a new association with is_primary = false
when another one with is_primary = false
existed, it fails like it's supposed to.
I solved it by using this for the validation:
validates :is_primary, uniqueness: {scope: :place_id}, if: ->{self.is_primary && self.class.where(place_id: self.place_id, is_primary: true).length > 0}
EDIT: I guess this works too, and it's simpler:
validates :is_primary, uniqueness: {scope: :place_id}, if: :is_primary