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

Allow the specification of resource class names for associative fields #1821

Closed
orenyk opened this issue Nov 5, 2020 · 4 comments
Closed

Comments

@orenyk
Copy link

orenyk commented Nov 5, 2020

  • What would you like to be able to do? Can you provide some examples?
    • I would like to create a HasMany field for an association whose name is not the same as the associated class name, e.g.
      class Foo < ApplicationRecord
        has_many :foo_bars
        has_many :pubs, through: :foo_bars, source: :bar
      end
      
      class FooBar < ApplicationRecord
        belongs_to :foo
        belongs_to :bar
      end
      If I add a field pubs: Field::HasMany without specifying class_name: Bar in the FooDashboard, Administrate will look for a PubDashboard instead of the BarDashboard. As per Automatic associations #1633 class_name is now deprecated, but all of the field should accept the resource_class option. However, it currently isn't being used to determine the class name for (at least) the HasMany field type.
  • How could we go about implementing that?
    • Leverage the non-deprecated resource_class option in the associative fields to determine the resource class if the association has a different name.
  • Can you think of other approaches to the problem?
    • Not at the moment

Thanks!

@pablobm
Copy link
Collaborator

pablobm commented Nov 5, 2020

Thank you for reporting this issue. In theory, the correct class should be detected through reflection at Administrate::Field::Associative, specifically here:

def self.associated_class_name(resource_class, attr)
reflection(resource_class, attr).class_name
end

Would you be able to have a look there and see what's going on?

@orenyk orenyk mentioned this issue Nov 5, 2020
@orenyk
Copy link
Author

orenyk commented Nov 5, 2020

Yeah let me do a little more digging and follow up. Thanks!

@cyu
Copy link
Contributor

cyu commented Feb 26, 2021

I'm seeing this too, on a has_one with a class_name: option. When we test reflection(resource_class, attr).class_name from the console we do get the expected class name.

I believe the issue has to do something with class load order – since we're declaring those attributes as statics on the dashboard class, they're getting loaded before the AR models are completed initialized. I'll try to make some time to dig into this deeper.

@pablobm
Copy link
Collaborator

pablobm commented Apr 6, 2023

I think this should be fixed by #2292

@pablobm pablobm closed this as completed Apr 6, 2023
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

3 participants