My discount class has sales_period. I want to write a method that can build this association when it does not exist, or update it when it exists. I am currently writing the following if condition.
class Discount < ActiveRecord::Base
has_one :sales_period
def fetch_period
end_date = ...
if sales_period.nil?
build_sales_period( end: end_date )
else
sales_period.end = end_date
end
end
end
Is there a better way to do this, alike find_or_create?
source
share