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

Sub-associations from polymorphic types don't work #1036

Open
jwoertink opened this issue May 8, 2024 · 0 comments
Open

Sub-associations from polymorphic types don't work #1036

jwoertink opened this issue May 8, 2024 · 0 comments
Labels
bug Something isn't working hacktoberfest Valid Issue for Hacktoberfest

Comments

@jwoertink
Copy link
Member

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.

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

@jwoertink jwoertink added the bug Something isn't working label May 8, 2024
@jwoertink jwoertink added the hacktoberfest Valid Issue for Hacktoberfest label Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hacktoberfest Valid Issue for Hacktoberfest
Projects
None yet
Development

No branches or pull requests

1 participant