luckyframework / luckyframework/avram
Sub-associations from polymorphic types don't work
- Dominant language
- Crystal
- Stars
- 183
- Forks
- 67
- PR merge metrics
- No merged PRs in 30d
Description
It appears we never actually test using polymorphic associations. If you try and call a method on them, you'll get a compile time error because Crystal can't determine the true shape of the object.
```crystal
table do
belongs_to user : User
belongs_to car : Car?
belongs_to computer : Computer?
polymorphic purchasable, associations: [:car, :computer]
end
purchase = PurchaseQuery.new.preload_purchasable.user_id(current_user.id).first
# We currently test that this works, and it does...
purchase.purchasable
# This however does not work
purchase.purchasable.owner!
# => Error: undefined method 'owner!' for User (compile-time type is BaseModel+)
```
In order to do this, you have to cast it to the proper type. `purchase.purchasable.as(Car).owner!`, but the whole point of the polymorphic association is that it could be any number of types so that gets a bit tricky
Contributor guide
Research direction
Start with the polymorphic association implementation and the existing tests that cover preloading and accessing purchasable. Reproduce the compile-time failure for purchase.purchasable.owner! and determine how supported association types should expose their methods. Done means polymorphic associations can call the appropriate sub-association without requiring a manual cast, with regression coverage for the shown case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- crystal
- Domain
- database
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 28/100