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

[Feature] When an ActiveForce object's association is eagerloaded it is an Array. When it is lazy loaded, it is ActiveForce::ActiveQuery #90

Open
bfrey08 opened this issue Mar 8, 2024 · 0 comments

Comments

@bfrey08
Copy link
Contributor

bfrey08 commented Mar 8, 2024

Describe the issue

This discrepancy (title) creates a scenario where if you have an implementation that relies on EagerLoading and LazyLoading you will get a different interaction. Lets take the following example:

What does this solve?

account = Salesforce::Account.includes({partner_opportunities: :owner}).first

and call

account.partner_opportunities.class

You will get

Array

When you want to filter (in memory) the partner_opportunities, you will do something like this:

account.partner_opportunities.select { |partner_opportunity| partner_opportunity.type == 'Some Random Type' }
=> [opportunity_1, opportunity_2]

Now lets say we use the same code in a different area of the APP except we want to lazyload instead of eagerlaod:

account = Salesforce::Account.first
account.partner_opportunities.class
=> ActiveForce::ActiveQuery

now lets try and filter this account:

account.partner_opportunities.select { |partner_opportunity| partner_opportunity.type == 'Some Random Type' }
=> error raised

ActiveForce::ActiveQuery implements it's own version of .select that does not work with an array's version of select. Hence we get an SOQL error.

Desired interaction

There should be no change in interaction if we lazyload or eagerload associations because sometimes we need to allow for both.

Current interaction

We can get around this issue by translating every relation into an Array before calling methods like .select on it.

account.partner_opportunities.to_a.select { |partner_opportunity| partner_opportunity.type == 'Some Random Type' }

This creates a confusing dev experience that is prone to errors when working with both lazy and eagerloaded objects.

Desired solution

In ActiveRecord they have a new object called ActiveRecord::Collection that would be returned in these scenarios. Something like this would be a better experience.

@bfrey08 bfrey08 changed the title [BUG] When an ActiveForce object's association is eagerloaded it is an Array. When it is lazy loaded, it is ActiveForce::ActiveQuery [FEATURE] When an ActiveForce object's association is eagerloaded it is an Array. When it is lazy loaded, it is ActiveForce::ActiveQuery Mar 8, 2024
@bfrey08 bfrey08 changed the title [FEATURE] When an ActiveForce object's association is eagerloaded it is an Array. When it is lazy loaded, it is ActiveForce::ActiveQuery [Bug] When an ActiveForce object's association is eagerloaded it is an Array. When it is lazy loaded, it is ActiveForce::ActiveQuery Mar 8, 2024
@bfrey08 bfrey08 changed the title [Bug] When an ActiveForce object's association is eagerloaded it is an Array. When it is lazy loaded, it is ActiveForce::ActiveQuery [Feature] When an ActiveForce object's association is eagerloaded it is an Array. When it is lazy loaded, it is ActiveForce::ActiveQuery Mar 8, 2024
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

1 participant