Closed TravisSiebenhaar closed 1 year 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
.
belongs_to_active_hash
?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?
@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.
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
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
I believe this was fixed with #272
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 anActiveRecord::Base
subclass, it continues to fail. This only occurs with myActiveHash
objects.