active-hash / active_hash

A readonly ActiveRecord-esque base class that lets you use a hash, a Yaml file or a custom file as the datasource
MIT License
1.2k stars 179 forks source link

Rails 7 finding a valid model association. #249

Closed TravisSiebenhaar closed 1 year ago

TravisSiebenhaar commented 2 years ago

I'm currently trying to upgrade my application from rails 6.1 to 7. When testing associations, my tests begin to fail. The error message is as follows:

ArgumentError: Rails couldn't find a valid model for RetailerChannelType 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.

I've tried the obvious first suggestion of adding the :class_name option on the declared association. However, because (correct me if I'm wrong) ActiveHash is not an ActiveRecord::Base subclass, it continues to fail. This only occurs with my ActiveHash objects.

kbrock commented 2 years ago

When I run the ActiveHash test suite with rails 7, I see similar errors.

Rails 7 is now validating the class_name for associations. This makes sense, but it throws a wrench into things for us since this gem is all about declaring models that specifically do not extend ActiveRecord::Base.

https://github.com/rails/rails/blob/7-0-stable/activerecord/lib/active_record/reflection.rb#L420-L424

TravisSiebenhaar commented 2 years ago

I am using belongs_to_active_hash. I did try adding extend ActiveHash::Associations::ActiveRecordExtensions alongside belongs_to_active_hash, even though that is basically what belongs_to_active_hash is doing anyways and that did not help.

@kbrock How are you handling the errors?

kbrock commented 2 years ago

@TravisSiebenhaar Sorry I can not help you with that.

I have not used this gem for years. But I used to use it a bunch, and have merge privileges, so I circle back and try and help where I can.

pfeiffer commented 2 years ago

We are using a monkey-patch for ActiveRecord to have compatibility with ActiveHash. It might be useful:


# config/initializers/active_hash_reflection.rb

module ActiveHashReflection
  extend ActiveSupport::Concern

  included do
    def compute_class(name)
      super
    rescue ArgumentError => e
      if e.message =~ /Please provide the :class_name option on the association/ && klass < ActiveHash::Base
        active_record.send(:compute_type, name)
      else
        raise
      end
    end
  end
end

Rails.application.reloader.to_prepare do
  ActiveRecord::Reflection::AssociationReflection.include ActiveHashReflection
end
akkt222 commented 1 year ago

Hello

seems that it doesnt work with through associations to me. I wrote something like this:

class  User < ApplicationRecord
  has_many :user_statuses
  has_many :statuses, through: :user_statuses
end

class Status < ActiveYamlBase
  include ActiveHash::Associations
  has_many :user_statuses
  has_many :users, through: :user_statuses
end

class < UserStatus < ApplicationRecord
  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to :user, class_name: 'User'
  belongs_to :status
end

then i expected, User.first.stasues works, but it actually returns ERROR like this: Please provide the :class_name option on the association declaration. if :class_name is already provided, make sure it's an ActiveRecord::Base subclass.)

I found a workaround like below, but i would appreciate if improved!


class  User < ApplicationRecord
  has_many :user_statuses
  def satuses
      user_statuses
        .map(&:status)
  end
end
flavorjones commented 1 year ago

I believe this was fixed with #272