I think you'll have better luck if you use a virtual attribute for the name, otherwise I think you'll run into problems when someone tries to treat a name like 'Bob Dobbs'
as an ObjectId. Something like this in your form:
<input name='invoice[contact_name]' type='text'>
and then in your Mongoid model:
attr_reader :contact_name
attr_accessible :contact_name
before_save do |invoice|
invoice.contact = Contact.find_by(name: invoice.contact_name)
end
That should get you the contact name in invoice_contact_name
as a String and nothing will try to interpret it as anything other than a string.
I'd probably drop the before_save
hook and move the Contact.find_by
into a before_validation
instead, then you can validate that you have a contact_id
and have a chance to detect a bad contact_name
:
attr_reader :contact_name
attr_accessible :contact_name
before_validation :lookup_contact_name
validate_presence_of :contact_id
def lookup_contact_name
# Some sort of `contact_name` cleanup might be a good idea in here too.
self.contact = Contact.find_by(name: contact_name)
end