-
-
Notifications
You must be signed in to change notification settings - Fork 277
fix: belongs_to field should use association primary_key #3665
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
d84d813
6694428
0eea6e0
3e4c15c
14ec146
7c46787
5840ba5
048f19d
fc2342e
e580342
22dcaf0
ec5253a
6b3d1cd
2795894
7f9840e
44ec64b
ab78711
ed2870b
c75c9ec
5ed4c6d
9f636f3
09d3d89
b9643e3
e0808bc
dd17f6b
74370e7
6504ea9
aff6f43
a1b9396
700db5b
b4d8dbe
7ef911e
912c9fd
d7663e1
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 | ||||
---|---|---|---|---|---|---|
|
@@ -115,6 +115,13 @@ def options | |||||
values_for_type | ||||||
end | ||||||
|
||||||
def primary_key | ||||||
@primary_key ||= reflection.association_primary_key | ||||||
# Quick fix for "Polymorphic associations do not support computing the class." | ||||||
rescue | ||||||
nil | ||||||
end | ||||||
|
||||||
def values_for_type(model = nil) | ||||||
resource = target_resource | ||||||
resource = Avo.resource_manager.get_resource_by_model_class model if model.present? | ||||||
|
@@ -126,6 +133,7 @@ def values_for_type(model = nil) | |||||
end | ||||||
|
||||||
query.all.limit(Avo.configuration.associations_lookup_list_limit).map do |record| | ||||||
# to_param uses slug so checking primary_key is unnecessary | ||||||
Nevelito marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
[resource.new(record: record).record_title, record.to_param] | ||||||
end.tap do |options| | ||||||
options << t("avo.more_records_available") if options.size == Avo.configuration.associations_lookup_list_limit | ||||||
|
@@ -212,14 +220,16 @@ def fill_field(record, key, value, params) | |||||
if valid_model_class.blank? || id_from_param.blank? | ||||||
record.send(:"#{polymorphic_as}_id=", nil) | ||||||
else | ||||||
record_id = target_resource(record:, polymorphic_model_class: value.safe_constantize).find_record(id_from_param).id | ||||||
found_record = target_resource(record:, polymorphic_model_class: value.safe_constantize).find_record(id_from_param) | ||||||
|
||||||
record_id = found_record&.send(primary_key.presence || :id) | ||||||
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.
Suggested change
This line must necessarily have a 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. Addressed here 912c9fd |
||||||
|
||||||
record.send(:"#{polymorphic_as}_id=", record_id) | ||||||
end | ||||||
else | ||||||
record_id = value.blank? ? value : target_resource(record:).find_record(value).id | ||||||
found_record = value.present? ? target_resource(record:).find_record(value) : nil | ||||||
|
||||||
record.send(:"#{key}=", record_id) | ||||||
record.send(:"#{key}=", found_record&.send(primary_key.presence || :id)) | ||||||
end | ||||||
|
||||||
record | ||||||
|
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 was a temporary fix for an error encountered during development. Could you remove this rescue and verify if the error still occurs? If it does, let's investigate the root cause and implement a more robust solution if possible. Thanks!