administrate
administrate copied to clipboard
HasMany field used for a relation (but not an association)
- What were you trying to do?
I've updated administrate to master and following #1633 some of my dashboards are broken. We are (ab)using the HasMany field for displaying relations that are not associations:
class Customer
def fellow_customers_that_are_hard_to_represent_with_an_association
Customer.scope.where...
end
end
class CustomerDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
fellow_customer: Field::HasMany.with_options(class_name: 'Customer'),
}
end
- What did you end up with (logs, or, even better, example apps are great!)?
ActionView::Template::Error:
undefined method `class_name' for nil:NilClass
Did you mean? class_eval
# ./vendor/ruby/2.7.0/bundler/gems/administrate-0f8624ca1d24/lib/administrate/field/associative.rb:15:in `associated_class_name'
# ./vendor/ruby/2.7.0/bundler/gems/administrate-0f8624ca1d24/lib/administrate/field/associative.rb:44:in `associated_class_name'
# ./vendor/ruby/2.7.0/bundler/gems/administrate-0f8624ca1d24/lib/administrate/field/associative.rb:37:in `associated_dashboard'
# ./vendor/ruby/2.7.0/bundler/gems/administrate-0f8624ca1d24/lib/administrate/field/has_many.rb:82:in `includes'
# ./vendor/ruby/2.7.0/bundler/gems/administrate-0f8624ca1d24/lib/administrate/field/has_many.rb:57:in `resources'
- What versions are you running?
- Rails: 6.0.3.2
- administrate: master
This pattern is still useful I think. I've tried converting those methods to associations and it's not always possible. Can the behaviour of the former HasMany be restored in a new Field type maybe?
Uh, that's an interesting use case; thank you for letting us know. My initial thought is that it could be solved with a new field type, although I'm not 100% sure. Would you be able to give it a go?