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

Belongs_to form NoMethodError when association uses different foreign_key #354

Closed
coneybeare opened this issue Dec 19, 2015 · 7 comments
Closed

Comments

@coneybeare
Copy link

Setup

class AuthorLink < ActiveRecord::Base
  belongs_to :author
  belongs_to :link_type, class_name: 'AuthorLinkType', foreign_key: :author_link_type_id
  ...
end

class AuthorLinkDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    link_type: Field::BelongsTo.with_options(class_name: "AuthorLinkType"),
    ...
  }
...
end

Problem

Visiting /admin/author_links/2/edit results in

NoMethodError

The NoMethodError stems from Field::BelongsTo.permitted_attribute, where you assume a standard association and hardcode _id accordingly.

def self.permitted_attribute(attr)
  :"#{attr}_id"
end

Solution

There needs to be more intelligence here to detect foreign_key variances amongst others. Agreed?

@zamith
Copy link

zamith commented Dec 21, 2015

Since we are passing the class_name as an option, we can pass the foreign_key as well, no?

Or we can try to be a bit more clever and do something like:

AuthorLink.reflect_on_association(:link_type).foreign_key
=> :author_link_type_id

which right now is not possible, since the field does not know about the AuthorLink class.

@coneybeare
Copy link
Author

I would prefer passing the option in the dashboard, but I know that the goal of administrate is to avoid DSL and keeping the with_options as limited as possible. This could be problematic because if you allow :foreign_key in the options, then you also should allow the other options for associations like :conditions, :through, :polymorphic etc...

@ACPK
Copy link

ACPK commented Jan 13, 2016

Any update or monkey patch for this so we can use Administrative?

@buren
Copy link
Contributor

buren commented Feb 21, 2016

@ACPK, @coneybeare I ran in into the same problem I fixed by adding those methods to the ActiveRecord model.

Here is something that could work in your case:

class AuthorLink < ActiveRecord::Base
  # ... 

  def link_type_id
  end

  def link_type_id=(id)
    self.link_type = AuthorLinkType.find(id)
  end
end

@angusfretwell
Copy link

Any update on this?

I found a slightly nicer workaround:

class Lister < User
  belongs_to :company, foreign_key: :organisation_id

  alias_attribute :company_id, :organisation_id
end

@dduqueti
Copy link

@angusfretwell Thank you!

@pablobm
Copy link
Collaborator

pablobm commented Feb 7, 2020

I think this was fixed with #807. I'm closing, but please reopen if you think differently.

@pablobm pablobm closed this as completed Feb 7, 2020
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

9 participants