Skip to content

fix: include has_one through associations in compute_select_fields in ResourcesGetter #732

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

Conversation

Pacyfik
Copy link
Contributor

@Pacyfik Pacyfik commented May 12, 2025

Hi 👋

Background

forest_liana 9.12.0 introduced a db query optimisation which includes specifying fields which are to be fetched from the database.
This seems to cause an error when requesting an has_one through association as one of the fields for the requested resource.

E.g.
Request params:

fields: { 'Tree' => 'id,name,location' }
class Tree < ActiveRecord::Base
  has_one :location, through: :island
end

Results in an error:

[2025-05-12 16:00:42 +0100] Forest 🌳🌳🌳  Records Index error: missing attribute: island_id
/Users/mat/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/activemodel-6.1.7.9/lib/active_model/attribute.rb:222:in 'ActiveModel::Attribute::Uninitialized#value'
        /Users/mat/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/activemodel-6.1.7.9/lib/active_model/attribute_set/builder.rb:55:in 'block in ActiveModel::LazyAttributeSet#fetch_value'
        /Users/mat/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/activemodel-6.1.7.9/lib/active_model/attribute_set/builder.rb:46:in 'Hash#fetch'
        /Users/mat/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/activemodel-6.1.7.9/lib/active_model/attribute_set/builder.rb:46:in 'ActiveModel::LazyAttributeSet#fetch_value'
        /Users/mat/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/activerecord-6.1.7.9/lib/active_record/attribute_methods/read.rb:30:in 'ActiveRecord::AttributeMethods::Read#read_attribute'
        /Users/mat/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/activerecord-6.1.7.9/lib/active_record/attribute_methods.rb:331:in 'ActiveRecord::AttributeMethods#[]'
        /Users/mat/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/activerecord-6.1.7.9/lib/active_record/associations/through_association.rb:81:in 'ActiveRecord::Associations::ThroughAssociation#stale_state'
[...]

Possibly unrelated to this, we also see errors related to fetching a belongs_to polymorphic association on a record. I am having a hard time replicating this issue using tests within the gem test suite. It's possible this is because the gem uses Rails 6.1.7.9 while we use Rails 8.0.2 in our app.
Request params:

fields: { 'Asset' => 'id,origin' }
class Asset < ApplicationRecord
  belongs_to :origin, polymorphic: true, autosave: true
end

Error:

Rails -- Exception: ActiveModel::MissingAttributeError: missing attribute 'origin_type' for Asset
/Users/mat/.rbenv/versions/3.4.3/lib/ruby/gems/3.4.0/gems/activemodel-8.0.2/lib/active_model/attribute.rb:251:in 'ActiveModel::Attribute::Uninitialized#value'
/Users/mat/.rbenv/versions/3.4.3/lib/ruby/gems/3.4.0/gems/activemodel-8.0.2/lib/active_model/attribute_set/builder.rb:43:in 'ActiveModel::LazyAttributeSet#fetch_value'
/Users/mat/.rbenv/versions/3.4.3/lib/ruby/gems/3.4.0/gems/activerecord-8.0.2/lib/active_record/attribute_methods/read.rb:33:in 'ActiveRecord::AttributeMethods::Read#read_attribute'
/Users/mat/.rbenv/versions/3.4.3/lib/ruby/gems/3.4.0/gems/activerecord-8.0.2/lib/active_record/attribute_methods.rb:416:in 'ActiveRecord::AttributeMethods#[]'
/Users/mat/.rbenv/versions/3.4.3/lib/ruby/gems/3.4.0/gems/activerecord-8.0.2/lib/active_record/associations/belongs_to_polymorphic_association.rb:8:in 'ActiveRecord::Associations::BelongsToPolymorphicAssociation#klass'
[...]

This PR fixes both issues.
I'd appreciate your opinions on this approach and would be happy to help validate any alternative ones.

Affected Versions

Forest agent (forest_liana) >= 9.12.0

Definition of Done

General

  • Write an explicit title for the Pull Request, following Conventional Commits specification
  • Test manually the implemented changes
  • Validate the code quality (indentation, syntax, style, simplicity, readability)

Security

  • Consider the security impact of the changes made

@nicolasalexandre9
Copy link
Member

Hi 👋 ,

Thank you very much for your feedback, I'll take a look at it and get back to you quickly.

@nicolasalexandre9
Copy link
Member

I noticed that the following gems have been added to the Gemfile:

  • concurrent-ruby
  • csv
  • drb

If this gems has only related to your fork, could you remove it from this PR

@Pacyfik
Copy link
Contributor Author

Pacyfik commented May 15, 2025

I noticed that the following gems have been added to the Gemfile:

  • concurrent-ruby
  • csv
  • drb

If this gems has only related to your fork, could you remove it from this PR

Hey. Yes, sorry. It seems csv and drb might have been removed from Ruby standard lib and I couldn't run tests without adding them to the gemfile. I'm using Ruby 3.4.1.
I'll remove all changes to gems from this PR.

@Pacyfik Pacyfik force-pushed the compute-select-fields-for-through-associations branch from 330f61b to de86be6 Compare May 15, 2025 08:55
@nicolasalexandre9
Copy link
Member

Indeed, the Csv gem has been removed from the standard library as of version 3.4.
We will add these gems in another PR soon.
Thanks again for your contribution.

@nicolasalexandre9 nicolasalexandre9 merged commit 6472522 into ForestAdmin:main May 15, 2025
2 of 3 checks passed
forest-bot added a commit that referenced this pull request May 15, 2025
## [9.12.1](v9.12.0...v9.12.1) (2025-05-15)

### Bug Fixes

* include has_one through associations in compute_select_fields in ResourcesGetter ([#732](#732)) ([6472522](6472522))
@forest-bot
Copy link
Member

🎉 This PR is included in version 9.12.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@Pacyfik Pacyfik deleted the compute-select-fields-for-through-associations branch May 15, 2025 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants