You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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.
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 trickyThe text was updated successfully, but these errors were encountered: