Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JamesAndresCM/0fafd66337e6e67631e0abd6e12cf44e to your computer and use it in GitHub Desktop.
Save JamesAndresCM/0fafd66337e6e67631e0abd6e12cf44e to your computer and use it in GitHub Desktop.
class InventoryItem < ApplicationRecord
belongs_to :location
accepts_nested_attributes_for :location
end
def location_attributes=(attrs)
begin
self.location_id = Location.find_by!(name: attrs[:name])
rescue ActiveRecord::RecordNotFound
super
end
end
# source: https://stackoverflow.com/questions/68418957/rails-nested-attributes-with-existing-records
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment