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

Multiple has_many/belong_to to same model. #275

Closed
shayneoneill opened this issue Aug 26, 2018 · 4 comments
Closed

Multiple has_many/belong_to to same model. #275

shayneoneill opened this issue Aug 26, 2018 · 4 comments

Comments

@shayneoneill
Copy link

I'm trying to represent an acyclic graph structure and need to be able use multiple has_many/belong_to refering back to the same table.

The problem is, even if I can specify the foreign_key in a belongs_to , I cant specify in the has_many what that belongs_to field is. In fact even in a simple relationship Owner 1 -> M Thing The foreign key set on thing is ignored by the has_many.

It doesnt seem to be helped either by the damn inflector changing what I'm writing (Seriously, that should be removed or at least give us an option to disable it, it was pretty high on the list of rails mistakes), but even if that was fixed, it'd still be pointing at the wrong field.

How do I specify manually the foreign key on the related table in has_many?

@drujensen
Copy link
Member

@shayneoneill I spent quite a bit of time adding the ability to override the field used on the has_many relationship but ran into the inflector not being available in a macro. It would have required some seriously bad coding to fix.

I am in agreement that it should be eliminated.

@shayneoneill
Copy link
Author

So its currently not possible to use has_many with a belong_to that specifies a field name?

@drujensen
Copy link
Member

not yet. you can always just create a method that handles the has_many. This is all the macro does.

def cars
  Granite::AssociationCollection(self, Car).new(self)
end

@drujensen
Copy link
Member

This issue will be addressed with #129

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants