Services Plugins FAQs

Dynamically update parent field based on calculation of value across child elements

I am doing a test project to learn about Bubble’s capabilities, and I can’t figure out something.

The test project is a Yelp clone. So basically, I have a parent “Restaurant” object with child “Review” objects. For relevance to this question, each Review object has 2 fields: a numeric Rating and a written Comment.

I want this Yelp clone to be map-focused, and I want the map markers for Restaurants to be color-coded based on the average rating across the child Reviews.

It seems that Bubble’s native Maps will not be able to populate different marker colors (please correct me if wrong), so I’m going to use the Mapbox plugin, which also has a workflow where a list of Restaurants can be populated with custom icons. After consulting with Mapbox, here’s how it will work:

  1. I will define an Option Set, named “AvgRating” that allows me to define a numeric set of ratings with distinct images (in my case, a colored marker)
  2. The Restaurant object will have a field that takes this AvgRating Option Set

Putting this together, the Mapbox workflow will list all Restaurants, and then pull the colored marker from the Restaurant’s AvgRating’s image.

That’s great, I can make that work.

HOWEVER >> now I need to know, how can I update the AvgRating field on the Restaurant object, as new child Reviews are added?

Is there a way to do this real time, so in a sense the AvgRating field becomes dynamic, or do I have to do some kind of daily background job/workflow?

ALSO, it’s important to mention that I’m totally open to completely new ways of resolving this, as this is just a learning test for me. As a developer, how I would have done this is a dynamic calculation where an icon is pulled based on a calculation of each parent Restaurant’s average rating across all child Reviews. But I’m not sure this is possible… hence the workaround to define a static AvgRating.

Hi @james4.

Thank you for reaching out and for your recent purchase! :blush:

Bubble supports dynamic expressions and calculations, so you can implement real-time updates without needing to store a static AvgRating field. Instead, you can dynamically calculate the average rating for each Restaurant when displaying it on the map.

1. Dynamically Calculate Average Rating:

  • Dynamically calculate it directly using Bubble’s expressions in your Mapbox workflow:
Search for Reviews:filtered:average of Rating
  • This ensures the average rating updates in real-time whenever new reviews are added.

2. Set Marker Colors Dynamically in Mapbox:

  • Use the Custom Icon feature of the Mapbox plugin.
  • In your workflow where you populate the map markers:
    • Add conditional logic to assign marker images based on the dynamically calculated average rating:
      • Example:
        • If the average rating is between 4-5, assign a green marker image.
        • If between 3-4, assign a yellow marker.
        • If below 3, assign a red marker.

This approach eliminates the need for background workflows and ensures the markers reflect the most current rating dynamically.

Alternative Static Option (If Necessary):

If you prefer a static field for simplicity:

  1. Add a workflow to calculate and update the AvgRating field on the Restaurant object whenever a new Review is created or modified.
  2. Use Backend Workflows to automate updates if needed for batch processing.

Additionally, please feel free to check out our demo page and documentation for guidance on setup and features.

If you’d like further clarification or help setting this up, feel free to share more details or reach out anytime! :blush:

Regards,
Support Team
Browse all Zeroqode Plugins for Bubble
Banner_Last3

Hi @james4 ,

I wanted to check if you’ve had a chance to review the previous message. :pray:

If you’re still experiencing any problems, please feel free to share a Loom video with us. We’re here to help and would love to assist you in resolving the issue.

Looking forward to your reply!

Regards,
Support Team
Browse all Zeroqode Plugins for Bubble
Banner_Last3