Skip to content

Commit

Permalink
fix: belongs_to field should use association primary_key
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Nov 14, 2024
1 parent c9de4df commit ce428c4
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/avo/fields/belongs_to_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def values_for_type(model = nil)
end

query.all.map do |record|
[resource.new(record: record).record_title, record.to_param]
[resource.new(record: record).record_title, record.send(primary_key)]
end
end

Expand Down Expand Up @@ -157,6 +157,10 @@ def is_polymorphic?
false
end

def primary_key
@primary_key ||= reflection.association_primary_key
end

def foreign_key
@foreign_key ||= if polymorphic_as.present?
polymorphic_as
Expand Down Expand Up @@ -211,12 +215,12 @@ 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
record_id = target_resource(record:, polymorphic_model_class: value.safe_constantize).find_record(id_from_param).send(primary_key)

record.send(:"#{polymorphic_as}_id=", record_id)
end
else
record_id = value.blank? ? value : target_resource(record:).find_record(value).id
record_id = value.blank? ? value : target_resource(record:).find_record(value).send(primary_key)

record.send(:"#{key}=", record_id)
end
Expand Down

0 comments on commit ce428c4

Please sign in to comment.