What is the correct way to do the following in Rails 4? I have a form that takes in a string and some other parameters for a particular model, let's say Foo. This model has an association to another model,
class Foo < ActiveRecord::Base
belongs_to :bar
end
I want to create a new Bar from the given string, assuming a Bar with that name doesn't already exist in the database.
My initial approach was to do this at the controller level, essentially grabbing the name from the params, and then attempting to create a object from it and mutate the original params to include the new object instead of the original string. But I'm realizing that that's not very idiomatic Rails and that I should instead be doing this entirely at the model level.
So how would I accomplish what I want to do at the model level? I am thinking I need a transient attribute and some kind of before_validation
filter, but I am not very familiar with Rails. Any ideas?