Rails 3 app model how ensure only one boolean field set to true at a time

后端 未结 8 839
误落风尘
误落风尘 2021-01-31 11:37

I have a Logo model that has fields of name:string, default:boolean. I want the true value to be unique, so that only one item in the database can be set to true at once. How do

8条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-31 12:18

    if you want this to work for creating and updating (rails v4) make note of this tidbit from rails guides

    after_save runs both on create and update, but always after the more specific callbacks after_create and after_update, no matter the order in which the macro calls were executed.

提交回复
热议问题