Skip to content

Commit

Permalink
Fix id getter of belongs_to in base.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
senhalil committed Jul 12, 2021
1 parent 26ffa37 commit 763f49a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions models/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ def self.belongs_to(name, options = {})
self[name] = val && (val.is_a?(Hash) ? c.create(val) : val)
end

define_method("#{name}_id") do
self[name]&.id
id_function_name = "#{name}_id".to_sym

define_method(id_function_name) do
self[id_function_name] ||= self[name]&.id
end

define_method("#{name}_id=") do |val_id|
define_method("#{id_function_name}=") do |val_id|
c = class_from_string(options[:class_name])
self[name] = val_id && c.find(val_id)
end
Expand Down
Loading

0 comments on commit 763f49a

Please sign in to comment.