https://avo.cool logo
Hi everyone, how are you? I'm not sure how to ...
# fields
m
Hi everyone, how are you? I'm not sure how to address the following: - I have a User STI model (AdminUser and PortalUser) - In AVO, I'm showing both AdminUser and PortalUser as separate resources. - the User model has_many :notifications, class_name: "Noticed::Notification", foreign_key: :recipient_id, inverse_of: :recipient, dependent: :destroy How should I set the has_many field in the PortalUser and AdminUser resources?
resource = Avo.resource_manager.get_resource_by_model_class(@reflection.active_record.name).new(params: helpers.params, view: view, user: helpers._current_user)
undefined method 'new' for nil
thanks!
m
Thread automatically created by Bruno Costanzo in #1141725718364045394
l
Hi @magnificent-jackal-1244 What resource do you use for
Noticed::Notification
?
m
Is a Notification resource,
field :recipient, as: :belongs_to, polymorphic_as: :recipient, types: [::PortalUser, ::AdminUser]
I've also tried with:
field :recipient, as: :belongs_to, polymorphic_as: :recipient, types: [::User]
Because the relation is polymorphic in the Notification model, but the User is just and STI.
l
What is the notification resource class?
Avo::Resources::Notification
?
m
class Avo::Resources::Notification < Avo::BaseResource self.model_class = ::Noticed::Notification
l
try
use_resource: "Avo::Resources::Notification"
on the
field :notifications, as: :has_many
field
m
With this change:
field :notifications, as: :has_many, use_resource: "Avo::Resources::Notification"
It still fails.
l
What is the error?
m
Copy code
undefined method 'new' for nil

      resource = Avo.resource_manager.get_resource_by_model_class(@reflection.active_record.name).new(params: helpers.params, view: view, user: helpers._current_user)
The thing is that the PortalUser resource is a child of User. So the relation with the Notification is in the User class. But we didn't define a User resource because we wanted them as separate resources.
Anyway, is not super important right now, but I read the docs to find how to fix it and I wasn't able.
field :notifications, as: :has_many, use_resource: "Avo::Resources::Notification" Is there an inverse_of option as well? it's defined in the model, but not in the resource.
l
Could you try this?:
Copy code
# config/initializers/avo.rb
Avo.configure do |config|
  config.model_resource_mapping = {
    'Noticed::Notification': 'Avo::Resources::Notification'
  }
end
m
No, still not working, the same issue:
Copy code
ruby
undefined method 'new' for nil

      resource = Avo.resource_manager.get_resource_by_model_class(@reflection.active_record.name).new(params: helpers.params, view: view, user: helpers._current_user)
l
do you have webconsole?
what is the output of
@reflection.active_record.name
?
m
I think the problem can be related with the fact that the has_many :notifications is defined in the User model, which is a STI for PortalUser and AdminUser. Then the Notifications uses polymorphism for a recipient_id.
is User
l
Might be it, can't be sure without reproducing and debugging
m
x >> @reflection.active_record.name => "User"
l
Ok, try to duplicate the association on the sti models
and remove it from the user
just to see if that's the problem
m
yes, that worked.
l
Thanks for confirming it
Do you also need it on the user model?
Or you can eject that association to a concern and incldue the concern on the subclasses?
m
I mean, every kind of user has_many notifications, I'm not sure if duplicating the relationship has any other implications.
I also noticed that the Notification -> Recipient Type = User Maybe moving the relationship will change this to PortalUser and AdminUser.
l
Got it
hmm
Could you set up a reproduction repo where I can put hands on this?
I think Avo should be able to find the resource from params or other context instead of relying on the reflection model
it should take a couple of minutes
and we might discover that is a bug, or maybe we figure out a fix for your use case and document it
m
Yes, I'll do it
happy to help and thank you for your help
l
sure Bruno, anytime!
m
l
Awesome! Thank you!
m
I added a GitHub issue and a Pull request that may work. But feel free to close both if it's not useful. I just had some free time and wanted to help. thanks!
l
that's awesome Bruno! really appreciate the initiative, I'll set up the reproduction repo and see if your approach fits
I would love to merge that PR, I'll let some feedback on the PR as soon as i test it
m
💯, thanks!