ActiveRecord belongs_to with an 'or' statement

61 Views Asked by At

I've got a bit of a strange system here where the app I'm developing has a local db, but also connects to an external db for some data.

One of the requirements was that if a manufacturer is not found in the external db, it have a temporary entry created in the local db until the external db can be updated (not under my control, but usually happens within 48 hours).

So, what I created was a temp_manufacturer table which would hold the details until the manufacturer is added to the external db.

That works fine, with the exception that products belong_to manufacturers, and I can't add a product it seems until the manufacturer is in the external db. In my products model I have

class Product < ActiveRecord::Base
        validates_uniqueness_of :sku
        validates_presence_of :manufacturer 

    has_many :categories
    belongs_to :manufacturer 
end

Of course, the problem here is that though I'm passing a manufacturer_id, the manufacturer_id is not in the manufacturer table in the external database.

What I was hoping to do was something like

validates_presence_of :manufacturer || :temp_manufacturer

belongs_to :manufacturer || :temp_manufacturer

but it doesn't seem like this is possible.

Is there another way to 'fake' a parent > child relationship like this?

Unfortunately it isn't possible for me to update the manufacturer table from my app, though I am able to get the id which will be used for the new manufacturer.

0

There are 0 best solutions below