Created
July 26, 2021 00:14
-
-
Save JamesAndresCM/0fafd66337e6e67631e0abd6e12cf44e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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