I am creating a foo obeject like this:
@foo = Foo.new(foo_params)
@foo.bar = Bar.where(name: "baz").first_or_create
But there are other objects that I will need to do this as well. So, I thought of overriding the Foo initialize method to do something like this:
class Foo < ApplicationRecord
def initialize(*args, BarName)
@foo = super
@foo.bar = Bar.where(name: BarName).first_or_create
end
end
and call it like this:
@foo = Foo.new(foo_params, "baz")
But Foo is an ApplicationRecord and it seems that it's not recommended to override the ApplicationRecord initialize method.
So how could I do this? Any other ideas? Would this initialize overriding thing work?