ActiveRecord validations, uniquness conditioning

Asked 1 years ago, Updated 1 years ago, 65 views


in ActiveRecord::Base validations Select multiple columns in uniquness and
Also, is it possible to add the value of the column to the conditions?

What do you want to be uniquness?
- user_id
- mst_category_id
- In addition, active=true

I thought it was like this, but it was completely different...

class User<ActiveRecord::Base

values:user_id,
unity: {
scope:[:mst_category_id, active:true]
}

ruby-on-rails rails-activerecord

2022-09-30 15:30

1 Answers

I didn't save it, but according to the reference,

validates:user_id, unique:{scope::mst_category_id}, conditions:->{where(active:true)}

There seems to be a way to write (conditions).


2022-09-30 15:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.