I am basically looking for the opposite of this:
Ruby on Rails: Building a child with default values when its parent is created
But I have not been able to find anything on stack overflow or the documentation to assist me with this.
class GeneralError < Details
#ASSOCIATIONS
belongs_to :type
belongs_to :error_log
has_one :exception
after_create :create_error_log
def create_error_log
self.error_log = ErrorLog.new(store_proc: "Columbia::GeneralError.log", type_id: 1, summary: "A general error was logged at: '#{Time.now}")
save
end
end
So general_errors belongs_to error_log. ErrorLog is also GeneralErrors header table in my database.
ErrorLog has a column called summary, where I would like to pass in a short description of the error that happened.
GeneralErrors I have a column named description, where I would like to pass in a longer description of what happened. Ultimately I would like to be able to call GeneralError.new() and pass in both summary AND description.
As of right now, with the code I listed above, I have been able to give ErrorLog default values every time I create a GeneralError. However, those values are hardcoded and not dynamic at all. What's the best, most dry way, to accomplish my task?
I discovered the answer to this a few days ago, but forgot to wrap this up.
I was thinking about the whole architecture of this wrong. Upon talking to the senior devs around my office we came to the conclusion that in most standard cases, you will want to create the header table first, and then create the child. Especially later on if you ever need to update or destroy something. I shouldn't have tried to create a header from a detail, but rather a detail from a header.