-
Notifications
You must be signed in to change notification settings - Fork 46
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
Fix association names when scaffolding join models if they don't match the table name #848
Changes from all commits
d4759bc
d0465a5
050fd29
56397e0
a1df68c
672ccca
88f5709
e62a33a
65abca6
52dc390
ee2cb70
15c41de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,20 @@ def association_class_name | |
name.split("_id").first | ||
end | ||
|
||
def plural_association_name | ||
association_class_name.tableize | ||
end | ||
|
||
def class_name_matches? | ||
# if namespaces are involved, just don't... | ||
# TODO: I'm not entirely sure that extracting this conditional was the right thing to do. | ||
# Are there scenarios where we want to assume a match even when namespaces are involved? | ||
if options[:class_name].include?("::") | ||
return false | ||
end | ||
name_without_id.tableize == options[:class_name].tableize.tr("/", "_") | ||
end | ||
Comment on lines
+65
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are the cases that this logic is for, I'm having a bit of a hard time understanding it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kaspth This is used in situations where you're scaffolding a relationship with a name that doesn't match the name of the table/model you're relating to. See the main description in this issue for an example: bullet-train-co/bullet_train#1508 (comment) |
||
|
||
def is_association? | ||
is_belongs_to? || is_has_many? | ||
end | ||
|
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.
tableize
will also pluralize, which I don't know if we want forbelongs_to
/has_one
associations. For those, we'd probably wantunderscore.tr("/", "_")
.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.
🤔 Hmm, good point. Currently I think this method is only being called in places where we have a
has_many
orhas_many through:
relationship, but I can see how this would be confusing and potentially lead to problems down the road. I'll clean it up a bit.