clear
clear copied to clipboard
Problem with has_many through relation if the middle class doesn't have two foreign keys
Current behavior of has_many through assume than the passthrough model table has two foreign keys bundled into the table.
However, in some case we want to call has_many through while the passed-through table has only one foreign key (or even non!).
An example would be this one:
class A
include Clear::Model
belongs_to b : B
has_many c : C, through: B # << Error because B doesn't have the foreign key for A and C
end
class B
include Clear::Model
has_many a : A
has_many c : C
end
class C
include Clear::Model
belongs_to b : B
end
This fix won't be trivial and must be done before we go to polymorphic associations.
I also have this issue
this was my workaround:
def views
View.query.inner_join("spaces"){ raw("views.space_id = spaces.id AND spaces.account_id = #{id}") }
end
I think the rails way of viewing has_many: :through is it covers any situation where there is some model A, some intermediate model, B, and some model C, where B has either a has_many, has_one, or belongs_to association with C, and A has either a has_many, has_one, or belongs_to association with B, and rails will do whatever joins are necessary to find all the rows of C that are indirectly associated with A. So something like ~~9~~ 4 different join situations I think potentially. Would <3 <3 <3 if this was fully supported.
I think the most common situation in the wild is not the one that is currently supported but rather A has_many B which has_many C, which my snippet above supports.
update: here are the possible situations (I think):
A=>has_many=>B=>has_many=>CA=>has_one | belongs_to=>B=>has_many=>CA=>has_many=>B=>has_one | belongs_to=>CA=>has_one | belongs_to=>Bhas_one | belongs_to=>C
The last one is the one the library currently supports I believe.