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

NoMethodError: undefined method relation #87

Closed
f440 opened this issue May 23, 2020 · 2 comments
Closed

NoMethodError: undefined method relation #87

f440 opened this issue May 23, 2020 · 2 comments
Assignees

Comments

@f440
Copy link
Contributor

f440 commented May 23, 2020

ActiveRecord (>=6.0.3) and AR::MultiTenant cause "NoMethodError: undefined method relation" when joining the multitenant tables and ActiveStorage tables.

The error has been occured since rails/rails#38929 is merged.

Steps to reproduce the issue:

  • Save this code to app.rb:
# frozen_string_literal: true

require "bundler/inline"

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

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

  # Activate the gem you are reporting the issue against.
  gem "rails", ENV["RAILS_VERSION"] || "6.0.3"
  gem "sqlite3"
  gem "activerecord-multi-tenant", require: false
end

require "active_record/railtie"
require "active_storage/engine"
require "tmpdir"
require "activerecord-multi-tenant"

class TestApp < Rails::Application
  config.root = __dir__
  config.hosts << "example.org"
  config.eager_load = false
  config.session_store :cookie_store, key: "cookie_store_key"
  secrets.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger

  config.active_storage.service = :local
  config.active_storage.service_configurations = {
    local: {
      root: Dir.tmpdir,
      service: "Disk"
    }
  }
end

ENV["DATABASE_URL"] = "sqlite3::memory:"

Rails.application.initialize!

require ActiveStorage::Engine.root.join("db/migrate/20170806125915_create_active_storage_tables.rb").to_s

ActiveRecord::Schema.define do
  CreateActiveStorageTables.new.change

  create_table :tenants
  create_table :groups, force: true do |t|
    t.integer :tenant_id, null: false
  end
  create_table :users, force: true do |t|
    t.integer :tenant_id, null: false
    t.integer :group_id, null: false
  end
end

class Tenant < ActiveRecord::Base
end

class Group < ActiveRecord::Base
  multi_tenant :tenant
  has_many :users
end

class User < ActiveRecord::Base
  multi_tenant :tenant
  belongs_to :group

  has_one_attached :profile
end

require "minitest/autorun"

class BugTest < Minitest::Test
  def test_join
    tenant = Tenant.create!
    group = Group.create!(tenant: tenant)

    User.create!(
      tenant: tenant,
      group: group,
      profile: {
        content_type: "text/plain",
        filename: "dummy.txt",
        io: ::StringIO.new("dummy"),
      }
    )

    assert_equal group.id, Group.joins(users: { profile_attachment: :blob }).find(group.id).id
  end
end
  • Compare the results of theese two runs:
    • RAILS_VERSION=6.0.2.2 ruby app.rb
    • RAILS_VERSION=6.0.3 ruby app.rb
@louiseGrandjonc
Copy link
Contributor

Hi,

Thanks for raising this issue. This should fix it :) : #88

@louiseGrandjonc louiseGrandjonc self-assigned this May 29, 2020
@louiseGrandjonc
Copy link
Contributor

Hi,

The PR #98 was merged and contains fixes for this and clears warning for rails 2.7.

A new version will be released soon.

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

2 participants