Any best practices for the following?:
I have Manufacturer model that has_many Inventory
In my new Inventory form I want a field that maps to Manufacturer.na
You may try like this:
class Inventory < ActiveRecord::Base
...
belongs_to :manufacturer
...
def manufacturer_name
manufacturer && manufacturer.name
end
def manufacturer_name=(value)
self.manufacturer = Manufacturer.find_by_name(value)
self.manufacturer ||= Manufacturer.new(:name => value)
end
...
end
In this case you should output manufacturer_name text field on Inventory form.