I have a validation that needs to apply only per a certain project that a user is in. The solution I came up with is to call this model method from the controller:
controller:
Client.validate_uniq_record(params[:project_id])
model:
def self.validate_uniq_record(proj_id)
validates_uniqueness_of :item, scope: :price, conditions: -> {where(project_id: proj_id)}
end
From reading other questions, it looks like passing a param to a model is a big nono: How to access params in the callback of a Rails model? I could not figure out how to do this through a virtual attribute either.
How would I validates_uniqueness_of
for only the current project the user is in?
Note, the Client
object has a project_id
column as an FK