-
Notifications
You must be signed in to change notification settings - Fork 55
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
Add association_primary to has_and_belongs_to_many #307
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job 👍 I'd only adjust one tiny thing. This feature definitely will be useful
Would you like me to make a spec? I haven't dug into your tests so idk what's their but i am sure this spec would be similar to whatever spec tests 'association_foreign'. |
@andrewc910 It would be great |
Okay, i think i did it. All i did was add I also change the Finally i added a comment which i plan to remove. Just wanted to mark it and see your thoughts. I couldn't quite understand the method |
Problem: When using Fix: This newest commit changes New Problem: **Note:**I don't believe there are explicit tests for these methods. This was something i discovered when refactoring some controllers. |
@@ -104,7 +111,8 @@ module Jennifer | |||
join_table!, | |||
{ | |||
foreign_field => obj.attribute(primary_field), | |||
association_foreign_key => rel.primary, | |||
association_foreign_key => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
association_primary ? rel.attribute(association_primary.not_nil!) : rel.primary
@@ -58,6 +60,7 @@ module Jennifer | |||
_primary = primary_field | |||
jt = join_table! | |||
q = query.join(jt, type: type) { Q.c(_primary) == c(_foreign) }.join(T, type: type) do | |||
# TODO: Replace `T.primary` with `association_primary_key`? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is correct
Yeah, my initial suggestion does work because |
First, let me say if this already exists and I am using jennifer wrong, please just let me know!
What does this PR do?
Adds
association_primary
tohas_and_belongs_to_many
. Currently, the query is built by defaulting to the primary key of the other table withT.primary
. This allows users to define a different column, likeuuid
, while keepingid
as the primary key for the table.Any background context you want to provide?
Right now i have an
App
& aUser
model. They are connected byhas_and_belongs_to_many
with the join table beingapps_users
.App psuedo sql table:
apps_users
sql table:Right now when i do:
It throws a bad query error. The sql looks like this:
Notice the
apps_users.app_id = apps.id
. A proper query would look like:In the user class i can do
association_foreign: :uuid
which is obviously wrong but changes the sql query to:Almost right. That's what led me down this rabbit hole. With this PR, i can change the above to
association_primary: :uuid
Release notes
Please fill the corresponding section regarding this pull request notes below instead of modifying
CHANGELOG.md
file. Please remove all redundant sections before posting request.Model
Adds
association_primary
tohas_and_belongs_to_many
relation to allow specifying a separate column for querying other than the primary key columnOther
I am not too happy with how
association_primary_key
came out inManyToMany
and would love to see a more elegant solution.