This may be a common mistake, but I have no idea how to search for it.
I have users that input records that can be either active or inactive. I want to cap the number of active records per user at 10. I do validations within the entry model, but I don’t know how to access the user’s current records from within the entry model.
class Entry < ApplicationRecord
belongs_to :user
validate :not_too_many_active_records
def not_too_many_records
<Something>.where(active: true).count < 10
end
end
I dropped a debugger in the not_too_many_records function, and user.entries.active.count correctly returns the number of active records (16 in my test case), but the validation still passes? The record is created even though 16 is not less than 10