{"id":102,"date":"2008-10-14T17:04:03","date_gmt":"2008-10-14T16:04:03","guid":{"rendered":"http:\/\/www.setfiremedia.com\/blog\/?p=102"},"modified":"2020-12-17T07:45:43","modified_gmt":"2020-12-17T06:45:43","slug":"rails-has-many-through-association-across-databases","status":"publish","type":"post","link":"https:\/\/www.setfiremedia.com\/blog\/rails-has-many-through-association-across-databases","title":{"rendered":"Rails: ‘Has_many through’ Association Across Databases"},"content":{"rendered":"

So recently I had the challenge of creating a ‘has_many through’ relationship across two databases.<\/p>\n

“Why would you do this?” you may ask. Well quite simply I am in a team building a new data management system to sit on top of a legacy system with its legacy database, we want to create something similar to https:\/\/docs.couchbase.com\/server\/current\/learn\/buckets-memory-and-storage\/buckets-memory-and-storage.html<\/a>. All the new code is new, shiny and streamlined and the old code is… well… crap but we have to keep both systems running concurrently so we have various tables in the legacy database we need to access from the new system. As it happens we need to access the legacy users table in a ‘has_many through’ from the new ‘orders’ table.
\n<\/p>\n

Set Up Your Secondary Database Connection<\/h3>\n

You can quite happily set up a model to connect do a database other then the default by setting the connection up in your ‘config\/database.yml’ as follows:<\/p>\n

legacy:\n  adapter: <adapter>\n  database: <database>\n  username: <username>\n  password: <password><\/pre>\n

And then in any model you want to use with your secondary database:<\/p>\n

<model>.establish_connection configurations['legacy']<\/pre>\n

Create Your ‘Through’ Model<\/h3>\n

Now a normal ‘has_many through’ just plain won’t work between models attached to two different databases but a normal has_many will. So we can create ‘has_many through’ functionality in the following way:<\/p>\n

Set up your ‘through’ model on either database. It really doesn’t matter which and set it to ‘belong_to’ your two main models.<\/p>\n

Set both main models to ‘has_many’ of your ‘through’ model.<\/p>\n

Create Your ‘Through’ Relationship<\/h3>\n

Use the following code in each of your main models to mimic the ‘has_many through’ association. In this example I’m using ‘orders’ and ‘users’ and my ‘through’ table is ‘order_users’:<\/p>\n

In ‘order’<\/p>\n

def users\n  user = []\n  order_users.each do |ou|\n    user << ou.user\n  end\n  user\nend<\/pre>\n

In ‘user’<\/p>\n

def orders\n  order = []\n  order_users.each do |ou|\n    order << ou.user\n  end\n  order\nend<\/pre>\n

And you’re done. Now the relationship will work just like any other ‘has_many through’.<\/p>\n","protected":false},"excerpt":{"rendered":"

So recently I had the challenge of creating a ‘has_many through’ relationship across two databases. “Why would you do this?” you may ask. Well quite simply I am in a team building a new data management system to sit on top of a legacy system with its legacy database, we want to create something similar […]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9,25,3],"tags":[],"_links":{"self":[{"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/posts\/102"}],"collection":[{"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":1,"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":690,"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions\/690"}],"wp:attachment":[{"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}