class Proverb < ApplicationRecord belongs_to :author belongs_to :user before_validation :setup_ownership attr_writer :author_name, :user_email def author_name author.try(:name) || @author_name end def user_email user.try(:email) || @user_email end private def setup_ownership if author author.update_attributes(name: author_name) else self.author = Author.new(name: author_name) author.identify_uniquely author.save end if user user.update_attributes(email: user_email.downcase) else self.user = User.create(email: user_email.downcase) end end def attributes super.merge(author_name: author_name, user_email: user_email) end end