Callbacks are a giant debate in the Rails world. I like them, but I also use them only when absolutely necessary. In this case, I don't think you need callbacks.
I'd recommend using find_or_create_by!
on your "base_url" table... Assuming your table is called BaseUrl
, you could do:
@base_url = BaseUrl.find_or_create_by!(:base_url => params[:base_url])
Again, this assumes the name of your table is BaseUrl
and you're using incoming params which contain a key of base_url
. You'd want to modify the above code to fit your needs, but I think find_or_create_by!
is what you want, as this will either:
a. Return an existing record with that base_url
, or
b. Create a new record with that base_url
.