clear icon indicating copy to clipboard operation
clear copied to clipboard

Polymorphic Associations/Relations

Open sdzyba opened this issue 6 years ago • 5 comments

Hi @anykeyh, first of all thank you for a great shard! Is there any plans to support polymorphic associations/relations similar to how it's done in Rails or Jennifer?

sdzyba avatar Jan 11 '19 18:01 sdzyba

Hello Slava!

Currently there's no support for polymorphic association. There's only support for inherited polymorphic table.

I think this feature will be added in the next set of improvements; the next release will be focus on stability however so don't expect it before March 😄

anykeyh avatar Jan 12 '19 03:01 anykeyh

Great news ! I follow up on this issue: In the 0.6 polymorphic association will be a reality ❤️ !!!

The only catch is it will return a "Clear::Model" at the relation, so casting / case when must be done after reception of the payload.

anykeyh avatar Feb 04 '19 08:02 anykeyh

Great to hear, thanks a lot!

The only catch is it will return a "Clear::Model" at the relation, so casting / case when must be done after reception of the payload.

Just wondering, what are the limitations that led to this? Seems like in Jennifer they accept a union of types for belongs_to relation in polymorphic model, which still requires you to cast to a specific type but at least it provides some type safety here. And for has_many :polymorphic_model they accept a concrete type, which is just awesome. Don't to want to seem impudent, it's just I like Clear a lot and I would love to switch to it :)

sdzyba avatar Feb 04 '19 10:02 sdzyba

I'll figure out; actually now I'm thinking to allow both: Union type and Clear::Model. Union type care of safety, while returning Clear::Model allows generalization and some extensions like acts-as-taggable-on

And for has_many :polymorphic_model they accept a concrete type, which is just awesome.

I don't understand this feature, can you explain me?

anykeyh avatar Feb 04 '19 10:02 anykeyh

actually now I'm thinking to allow both: Union type and Clear::Model

Sounds great!

I don't understand this feature, can you explain me?

I meant you can specify something like that in Jennifer. has_many :pictures, Picture, polymorphic: true, inverse_of: :imageable Where Picture has imageable_id and imageable_type columns.

I just thought you meant here it will return Clear::Model as well instead of a specified class, or I'm wrong here?

sdzyba avatar Feb 04 '19 10:02 sdzyba