10000 When an associated record has a composite key, the resource 'show page' fails · Issue #8716 · activeadmin/activeadmin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

When an associated record has a composite key, the resource 'show page' fails #8716

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

Closed
glenpike opened this issue May 7, 2025 · 0 comments
Closed

Comments

@glenpike
Copy link
glenpike commented May 7, 2025

We have a model that belongs to another model which has a composite key, e.g. (fictitious key) [place_id, event_hour, event_min]

belongs_to :place, class_name: "Place", foreign_key: Place.primary_key, optional: true

Expected behavior

The page should load

Actual behavior

When we try to load the page for the model we get an error undefined method 'to_sym' for an instance of Array

#<ActionView::Template::Error: undefined method 'to_sym' for an instance of Array>
/path/to/ruby/version/lib/ruby/gems/3.4.0/gems/activeadmin-3.3.0/lib/active_admin/resource/attributes.rb:24:in 'block in ActiveAdmin::Resource::Attributes#foreign_methods'

https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource/attributes.rb#L24

r.foreign_key.to_sym chokes on the foreign key [place_id, event_hour, event_min]

Suggestions:

If that is a column to view / show, then allow it to handle arrays:

    index_by do |r|
      key = r.foreign_key
      key.is_a?(Array) ? key.join("_").to_sym : key.to_sym
    end

Or reject any non-standard keys so we don't show those columns / links?

def foreign_methods
  @foreign_methods ||= resource_class.reflect_on_all_associations.
    select { |r| r.macro == :belongs_to }.
    reject { |r| r.chain.length > 2 && !r.options[:polymorphic] }.
    reject { |r| !r.foreign_key.is_a?(String) }.
    index_by { |r| r.foreign_key.to_sym }
end

### How to reproduce

Having a way to reproduce your issue will help people confirm, investigate,
and ultimately fix your issue. You can do this by providing an executable test
case. To make this process easier, please use [our bug report template script].

Copy the content of the appropriate template into an .rb file and make the
necessary changes to demonstrate the issue. You can execute it by running
ruby the_file.rb in your terminal. If all goes well, you should see your test
case failing.

[our bug report template script]: https://github.com/activeadmin/activeadmin/blob/master/tasks/bug_report_template.rb
Sorry, I haven't had time to reproduce. If needed, I will try to do that, but hopefully the issue is apparent above.

@activeadmin activeadmin locked and limited conversation to collaborators May 27, 2025
@javierjulio javierjulio converted this issue into discussion #8730 May 27, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0