anlek / mongify

Mongify allows you to map your data from a sql database and into a mongodb document database.
http://github.com/anlek/mongify
MIT License
317 stars 82 forks source link

Using before_save with reference to another collection #157

Closed robertangeles closed 6 years ago

robertangeles commented 6 years ago

Hi @anlek , Hope you are well. I was wondering if you can eye-ball this translation file I'm trying to create. Probably there's another way to do it. Please see the translation below.

table "staff" do column "id", :key column "staffId", :string column "staffFirstName", :string column "staffSurname", :string column "staffRelUnit", :string column "staffTitle", :string column "ahPhone", :string column "bhPhone", :string column "mobPhone", :string column "emailAdd", :string column "pAddressLine1", :string column "pSuburb", :string column "pState", :string column "pPostCode", :string column "sAddressLine1", :string column "sSuburb", :string column "sState", :string column "sPostCode", :string column "staffStatus", :string end

table "properties" do column "id", :key column "propId", :string column "propType", :string column "propCategory", :string column "addressLine1", :string column "suburb", :string column "state", :string column "postCode", :string column "rent", :string column "bondAmount", :string column "bedrooms", :string column "bathrooms", :string column "carAccom", :string column "isFurnished", :string column "isPetsAllowed", :string column "laundry", :string column "kitchen", :string column "cooling", :string column "heating", :string column "cooking", :string column "ensuites", :string column "cooling", :string column "heating", :string column "cooking", :string column "ensuites", :string column "study", :string column "construction", :string column "latitude", :string column "longitude", :string column "adHead", :string column "adCopy", :string column "leasingexecid", :integer, :references => :staff column "leasingexecname", :string column "newbusexecid", :integer, :references => :staff column "newbusexecname", :string column "ownerrelexecid", :integer, :references => :staff column "ownerrelexecname", :string

before_save do |row| row.propAddress = {} row.propAddress['addressLine1'] = row.delete('addressLine1') row.propAddress['suburb'] = row.delete('suburb') row.propAddress['state'] = row.delete('state') row.propAddress['postCode'] = row.delete('postCode') row row.propFeature = {} row.propFeature['rent'] = row.delete('rent') row.propFeature['bondAmount'] = row.delete('bondAmount') row.propFeature['laundry'] = row.delete('laundry') row.propFeature['bedrooms'] = row.delete('bedrooms') row.propFeature['bathrooms'] = row.delete('bathrooms') row.propFeature['carAccom'] = row.delete('carAccom') row.propFeature['isFurnished'] = row.delete('isFurnished') row.propFeature['isPetsAllowed'] = row.delete('isPetsAllowed') row.propFeature['kitchen'] = row.delete('kitchen') row.propFeature['cooling'] = row.delete('cooling') row.propFeature['heating'] = row.delete('heating') row.propFeature['cooking'] = row.delete('cooking') row.propFeature['study'] = row.delete('study') row.propFeature['ensuites'] = row.delete('ensuites') row.propFeature['construction'] = row.delete('construction')
row row.propLocation = {} row.propLocation['latitude'] = row.delete('latitude') row.propLocation['longitude'] = row.delete('longitude') row row.propStaff = {} row.propStaff['leasingexecid'] = row.delete('leasingexecid') row.propStaff['leasingexecname'] = row.delete('leasingexecname') row end

end

When I tried embedding field with a reference to another collection, it does not work. It only displays the integer id instead of the ObjectID from the staff collection but if I do not embed it in a document, it works and displays the objectID from the staff collection. My apologies, I haven't mastered creating translation files yet with mongify. Is there another way of doing this?

Thanks!

Rob

anlek commented 6 years ago

Hey Rob, I see you closed this ticket, does that mean you figured it out?

robertangeles commented 6 years ago

Hi Andrew. Hope you are well. Yes, I did figure it out. The answer was starring at me 😊

It was fun experience. I basically created another view from SQL Server and used:

table "table_name",
:embed_in => 'users',
:on => 'owner_id',
:as => 'object'

On a side note, I was wondering if you can explain what a polymorphic table is?

Thanks!

anlek commented 6 years ago

Awesome, glad it worked out!

You can read about polymorphic associations (and tables) here: https://launchschool.com/blog/understanding-polymorphic-associations-in-rails

robertangeles commented 6 years ago

Thanks Andrew!