Skip to content

The has_many :through Association does not get record #239

@ghost

Description

Steps to reproduce

Run below code.

# frozen_string_literal: true

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem 'activerecord', '~> 6.1.6'
  gem 'active_hash', '~> 3.2.1'

  gem 'sqlite3'
end

require 'active_record'
require 'active_hash'

require 'minitest/autorun'
require 'logger'

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :appointments, force: true do |t|
    t.references :physician
    t.references :patient
  end

  create_table :patients, force: true do |t|
  end
end

class Physician < ActiveHash::Base
  include ActiveHash::Associations

  has_many :appointments
  has_many :patients, through: :appointments

  self.data = [
    {:id => 1, :name => "ikeda"},
    {:id => 2, :name => "sato"}
  ]
end

class Appointment < ActiveRecord::Base
  extend ActiveHash::Associations::ActiveRecordExtensions

  belongs_to :physician
  belongs_to :patient
end

class Patient < ActiveRecord::Base
  extend ActiveHash::Associations::ActiveRecordExtensions

  has_many :appointments
  has_many :physicians, through: :appointments
end

class TestAssociation < Minitest::Test
  def test_association
    physician = Physician.first
    patient = Patient.create!
    Appointment.create!(physician: physician, patient: patient)

    assert_equal [physician], patient.appointments.flat_map(&:physician) # works
    assert_equal [physician], patient.physicians # error
  end
end

Expected behavior

The script above should produce no errors, and get physician.

Actual behavior

NoMethodError: undefined method `relation_delegate_class' for Physician:Class

System configuration

Ruby version: 2.7.4
ActiveHash version: 3.1.0

updated:
Ruby version: 3.0.6
ActiveHash version: 3.2.1

updated by @kbrock to bring up to current version of ruby, rails, and active hash

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions