ActiveRecord::Base subclass requirement
dvodvo opened this issue · 0 comments
An multi-tenant application has various classes relying on ActiveStorage (tenant is defined by class Shop).
Validation values are stored in a db table for each class, by tenant.
In the basic set-up, before validations,
class Document < ApplicationRecord
belongs_to :individual, optional: true
belongs_to :union, optional: true
validate :at_least_one_identifier
has_one_attached :file
# validates :file, limit: { max: -> (record) { record.shop.document_validation.size_less_than }, message: t('is too large') }
def at_least_one_identifier
if [self.individual_id, self.union_id].reject(&:blank?).size == 0
errors[:base] << (I18n.t('picture.one_identifier'))
end
end
end
Creating an attached file runs as expected and the view renders. However the proc declaration for tha validation returns the error Rails couldn't find a valid model for Document association. Please provide the :class_name option on the association declaration. If :class_name is already provided, make sure it's an ActiveRecord::Base subclass.
The class structures are:
class Individual < ApplicationRecord
belongs_to :shop
[...]
class Union < ApplicationRecord
belongs_to :shop
[...]
class Shop < ApplicationRecord
has_one :document_validation
[...]
class DocumentValidation < ApplicationRecord
belongs_to :shop
The latter category stores values for validating documents of the shop's class.
Given that document can belong to either Individual or Union, also attempted was:
validates :file, limit: { max: -> (record) { record.individual.shop.document_validation.size_less_than }, message: t('is too large') }
but leading to the same error.
What I fail to understand is how the attached object can be created with the rails-generated scaffolding, but the validation cannot run (and obviously how to get out of the conundrum).