Giter Site home page Giter Site logo

Comments (6)

pfeiffer avatar pfeiffer commented on July 16, 2024 3

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

from active_hash.

flavorjones avatar flavorjones commented on July 16, 2024 1

I believe this was fixed with #272

from active_hash.

kbrock avatar kbrock commented on July 16, 2024

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

from active_hash.

TravisSiebenhaar avatar TravisSiebenhaar commented on July 16, 2024

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?

from active_hash.

kbrock avatar kbrock commented on July 16, 2024

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

from active_hash.

akkt222 avatar akkt222 commented on July 16, 2024

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

from active_hash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.