Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upLegacy AR database -- polymorphic association #73
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Hi Piotr! Thanks for the gist ! How would you query from |
This comment has been minimized.
This comment has been minimized.
@blelump updated the gist with |
This comment has been minimized.
This comment has been minimized.
@solnic , thanks for update ! However, I'll dig deeper ;-) . What if you dont know where particular tag is bound to? Say you want you want exactly
That may be difficult and I imagine you'd achieve the above entity with two separated queries and then merge the result with Transproc features. Is there any other solution (by just utilizing ROM)? |
This comment has been minimized.
This comment has been minimized.
@blelump yes it's possible but requires custom relation composition, as associations don't support overridding query logic (you can only extend them via |
This comment has been minimized.
This comment has been minimized.
I marked it as a feature for v2.0.0 milestone. We don't want this to be a first class feature, but it can be a plugin that people will be able to turn on when they need to connect to a legacy, post-AR database schema. I imagine something like this could be done: class Songs < ROM::Relation[:sql]
schema(infer: true) do
use :ar_polymorphic_associations
associations do
has_many :taggings, foreign_key: :taggable_id
has_many :tags, as: :taggable, through: :taggings
end
end
end
class Tags < ROM::Relation[:sql]
schema(infer: true) do
use :ar_polymorphic_associations
associations do
belongs_to :taggable, polymorphic: true
end
end
end |
This comment has been minimized.
This comment has been minimized.
schema plugins? I started to think they'll be useful for auto-indexing and other type-manipulation things |
This comment has been minimized.
This comment has been minimized.
@solnic , thanks in advance for another gist ! I completetely understand you don't want it as a basic feature. IMO it doesn't have to be a plugin either if the overall complexity of the acceptable solution is a few extra LoC. The API should be clear and straightforward, but on the other hand there're other places for sure that need one/your's attention (& improvements). |
This comment has been minimized.
This comment has been minimized.
@blelump ayee, hence "help-wanted" label @flash-gordon yeah I think we need schema plugins, as schemas are separate, standalone objects, so extending them through relation plugins would be awkward :) |
This comment has been minimized.
This comment has been minimized.
@solnic exactly! |
This comment has been minimized.
This comment has been minimized.
Hello @solnic , sorry for the remainder, any progress on the |
This comment has been minimized.
This comment has been minimized.
No, sorry. Had unexpected errands to run and zero time for oss today. I'll try to tackle it tomorrow.
Thanks,
Piotr
…On 8 Mar 2017, 5:44 PM +0100, Michał Pietrus ***@***.***>, wrote:
Hello @solnic (https://github.com/solnic) , sorry for the remainder, any progress on the taggable ?
|
This comment has been minimized.
This comment has been minimized.
Hi @solnic , I've tried to investigate it further and as I understand, at least from the public API, I need to provide another relation in order to 'combine' the current relation with something. However, I imagine, to make it working, I need to 'edit' current relation somehow (without another relation since it's not known yet -- I mean whether it's a book or a song) and append something to it, based on |
This comment has been minimized.
This comment has been minimized.
Hey @solnic, I have some models that use polymorphism in mongoid that I want to move over. The way I do it is: where media can be any type of model. What is the best way to do this? I don't want 10 different belongs_to. |
This comment has been minimized.
This comment has been minimized.
@jnylen there's a chance it'll be supported in rom 4, if not then 4.1. It's low priority at the moment, so it needs to wait. |
This comment has been minimized.
This comment has been minimized.
Closing this. We'll keep this in mind and eventually make this possible. |
Hello!
Since ROM API (and docs!) is going better and better with each release, we'd like to taste it for real. However, carrying a legacy AR DB needs some solution for 'polymorphic association' antipattern I haven't found. I've seen this and this, but none of them offer complete answer, at least for me. Let's continue with the code snippet given here and say that I have
Snapshot
and want to get theQuestions
, based on theparent_type
attribute. In particular:So the result is an aggregate of snapshot and whatever is given within the polymorphic association. In AR world, the aggregate's child is resolved based upon the
parent_type
column, but how to specify relation name based on that column value?I'm not expecting any one-line solution, I'd be happy with any😄 .