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

Stack level too deep - when belongs_to and foreign_key have the same name #26778

Closed
olavnyy opened this issue Oct 13, 2016 · 7 comments
Closed

Stack level too deep - when belongs_to and foreign_key have the same name #26778

olavnyy opened this issue Oct 13, 2016 · 7 comments

Comments

@olavnyy
Copy link

@olavnyy olavnyy commented Oct 13, 2016

Steps to reproduce

class CreateAccounts < ActiveRecord::Migration[5.0]
  def change
    create_table :accounts do |t|
      t.string :title
      t.integer :created_by

      t.timestamps
    end
  end
end

class CreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
      t.string :name

      t.timestamps
    end
  end
end

class Account < ApplicationRecord
  belongs_to :created_by, :class_name => "User", :foreign_key => "created_by"
end

class User < ApplicationRecord
end

Account.create(title: "Test", created_by: User.create(name: "TestUser"))
Account.where(created_by: User.first)

Expected behavior

ActiveRecord_Relation should be returned

Actual behavior

A stack level too deep error is raised

System configuration

Rails 5.0.0.1

Ruby 2.3.1

Work in Rails 4.2

@utilum

This comment has been minimized.

Copy link
Contributor

@utilum utilum commented Oct 13, 2016

The foreign key is supposed to be the column name from associated table... don't you want a column from the users table in there?

@BEaStia

This comment has been minimized.

Copy link

@BEaStia BEaStia commented Oct 14, 2016

According to the sources, you were to write:

class Account < ApplicationRecord
  belongs_to :user, :class_name => "User", :foreign_key => "created_by"
end
@yivo

This comment has been minimized.

Copy link

@yivo yivo commented Oct 23, 2016

The attribute created_by exists on Account. You are trying to define association with the same name. There is no functionality in Rails to have same attribute and association names. This is your typo and is not related to Rails.

You should rename attribute created_by or change association name as @BEaStia mentioned.

@yivo

This comment has been minimized.

Copy link

@yivo yivo commented Oct 23, 2016

The convention in Rails is that you should add postfix _id to table keys.
created_by => created_by_id

@milep

This comment has been minimized.

Copy link

@milep milep commented Dec 16, 2016

I got the same issue with a legacy database, which had a foreign_key with a same name as referenced table/model.
Eg. in class PaymentRows:
belongs_to :payment, foreign_key: 'payment'

@spidergears

This comment has been minimized.

Copy link

@spidergears spidergears commented Aug 18, 2019

@rafaelfranca I have this very issues today with rails 5.2.3 .
Is this a won't fix ?

@spidergears

This comment has been minimized.

Copy link

@spidergears spidergears commented Aug 18, 2019

For anyone coming to this, currently a simple workaround is to change the name of the association and specify class_name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants
You can’t perform that action at this time.