Title: hyper-model reflects on associations of sibling STI class · Issue #275 · hyperstack-org/hyperstack · GitHub
Open Graph Title: hyper-model reflects on associations of sibling STI class · Issue #275 · hyperstack-org/hyperstack
X Title: hyper-model reflects on associations of sibling STI class · Issue #275 · hyperstack-org/hyperstack
Description: This is only an issue when using STI classes that have their own associations. An example: class Owner < ApplicationRecord has_many :pets end class Bone < ApplicationRecord belongs_to :dog end class ScratchingPost < ApplicationRecord bel...
Open Graph Description: This is only an issue when using STI classes that have their own associations. An example: class Owner < ApplicationRecord has_many :pets end class Bone < ApplicationRecord belongs_to :dog end clas...
X Description: This is only an issue when using STI classes that have their own associations. An example: class Owner < ApplicationRecord has_many :pets end class Bone < ApplicationRecord belongs_to :dog en...
Opengraph URL: https://github.com/hyperstack-org/hyperstack/issues/275
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"hyper-model reflects on associations of sibling STI class","articleBody":"This is only an issue when using STI classes that have their own associations.\r\n\r\nAn example:\r\n\r\n```ruby\r\nclass Owner \u003c ApplicationRecord\r\n has_many :pets\r\nend\r\n\r\nclass Bone \u003c ApplicationRecord\r\n belongs_to :dog\r\nend\r\n\r\nclass ScratchingPost \u003c ApplicationRecord\r\n belongs_to :cat\r\nend\r\n\r\nclass Pet \u003c ApplicationRecord\r\n belongs_to :owner\r\nend\r\n\r\nclass Dog \u003c Pet\r\n has_many :bones\r\nend\r\n\r\nclass Cat \u003c Pet\r\n has_many :scratching_posts\r\nend\r\n\r\nPet.reflect_on_all_associations\r\n# Currently\r\n#=\u003e [{ belongs_to: :owner, ... }, { has_many: :bones, ... }, { has_many: :scratching_posts, ... }]\r\n# Should be\r\n#=\u003e [{ belongs_to: :owner, ... }]\r\n\r\nDog.reflect_on_all_associations\r\n# Currently\r\n#=\u003e [{ belongs_to: :owner, ... }, { has_many: :bones, ... }, { has_many: :scratching_posts, ... }]\r\n# Should be\r\n#=\u003e [{ belongs_to: :owner, ... }, { has_many: :bones, ... }]\r\n\r\nCat.reflect_on_all_associations\r\n# Currently\r\n#=\u003e [{ belongs_to: :owner, ... }, { has_many: :bones, ... }, { has_many: :scratching_posts, ... }]\r\n# Should be\r\n#=\u003e [{ belongs_to: :owner, ... }, { has_many: :scratching_posts, ... }]\r\n```\r\n\r\nThe code that is doing this is \r\n\r\n```ruby\r\n def self.reflect_on_all_associations\r\n base_class.instance_eval { @associations ||= superclass.instance_eval { (@associations \u0026\u0026 @associations.dup) || [] } }\r\nend\r\n```\r\n\r\nIt always reflects on the base_class, so that and all the inheriting get all the associations. This causes issues where hyper-model is trying to instantiate associations on the client, but they don't exist on that class but maybe its sibling's class the an error is thrown and the page goes blank.\r\n\r\nI'm thinking this was done before STI was implemented and the `type` column was looked at and used. But since that has been implemented, this should be fixed so that only the proper associations are reflected on and used.\r\n\r\nA fix would be\r\n```ruby\r\ndef self.reflect_on_all_associations\r\n @associations ||= superclass.instance_eval { (@associations \u0026\u0026 @associations.dup) || [] }\r\nend\r\n```\r\n\r\nRun locally, all specs pass.","author":{"url":"https://github.com/adamcreekroad","@type":"Person","name":"adamcreekroad"},"datePublished":"2019-11-04T16:10:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/275/hyperstack/issues/275"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8d8d5627-058d-20d2-6f9f-e0081011f5a3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C970:D3A00:2CB16B0:3919299:6991BBEB |
| html-safe-nonce | ca721d54a6b4aae384f81be0284ee980d418bcd8d3063641b04bf3ce70eaa139 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOTcwOkQzQTAwOjJDQjE2QjA6MzkxOTI5OTo2OTkxQkJFQiIsInZpc2l0b3JfaWQiOiI0MDMxNzg0NzYyMTE4ODU4NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b0503f9d5b78bf9c0e3ec065fb7f7cdb446d6e94486aab6131f68d565134f22b |
| hovercard-subject-tag | issue:517252593 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/hyperstack-org/hyperstack/275/issue_layout |
| twitter:image | https://opengraph.githubassets.com/78b871c65963fed9b88a1d3a10cb1f149ace725b964996aabe0aadb7c8879764/hyperstack-org/hyperstack/issues/275 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/78b871c65963fed9b88a1d3a10cb1f149ace725b964996aabe0aadb7c8879764/hyperstack-org/hyperstack/issues/275 |
| og:image:alt | This is only an issue when using STI classes that have their own associations. An example: class Owner < ApplicationRecord has_many :pets end class Bone < ApplicationRecord belongs_to :dog end clas... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | adamcreekroad |
| hostname | github.com |
| expected-hostname | github.com |
| None | 42c603b9d642c4a9065a51770f75e5e27132fef0e858607f5c9cb7e422831a7b |
| turbo-cache-control | no-preview |
| go-import | github.com/hyperstack-org/hyperstack git https://github.com/hyperstack-org/hyperstack.git |
| octolytics-dimension-user_id | 34562730 |
| octolytics-dimension-user_login | hyperstack-org |
| octolytics-dimension-repository_id | 145879576 |
| octolytics-dimension-repository_nwo | hyperstack-org/hyperstack |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 145879576 |
| octolytics-dimension-repository_network_root_nwo | hyperstack-org/hyperstack |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 848bc6032dcc93a9a7301dcc3f379a72ba13b96e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width