Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord::Base subclass requirement #207

Open
dvodvo opened this issue Oct 28, 2023 · 0 comments
Open

ActiveRecord::Base subclass requirement #207

dvodvo opened this issue Oct 28, 2023 · 0 comments

Comments

@dvodvo
Copy link

dvodvo commented Oct 28, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant