0

I am trying to implement the HATBM association between the two models of which one model points to my local database table and other model is pointing to the table of an external database.

local model :

class Group < ApplicationRecord
  has_and_belongs_to_many :chains
end

Model connecting to an external database :

class Chain < ApplicationRecord
  establish_connection :xyz
  self.table_name = 'queues'    
  has_and_belongs_to_many :groups       
end

Join table(chains_groups) fields:

1) :group_id 
2) :chain_id

Application details:

Rails : 5.2.0
Database : postgresql

Whenever I try to insert data in join table I am getting an error ActiveRecord::StatementInvalid stating that chains_groups table doesn't exist. It seems like that, it is always searching for the join table (chains_groups) on external database which is present in my local database. I even try to implement "has_many :through" association but the result is the same.

2017kamb
  • 192
  • 3
  • 8
  • 27
Vivek
  • 1
  • 3
  • How are you inserting data into the join table? – BM5k Jul 13 '18 at 00:33
  • Using << operator. group = find group group.chains << Chain.where('id in (?)', ids) – Vivek Jul 13 '18 at 02:58
  • on which side of the association? – BM5k Jul 13 '18 at 03:01
  • Issue occur when trying to insert data from "Group" model side. – Vivek Jul 13 '18 at 03:19
  • I've attempted to reproduce with a [simple rails app](https://gitlab.com/snippets/1732929) but I am seeing a slightly different error: `relation "groups_queues" does not exist`. When I rename the join table, I don't see the error. – BM5k Jul 13 '18 at 23:40
  • Unfortunately, I still don't think it's working the way you expect it to. Both sides of the association return a CollectionProxy. When accessed via `chain.groups`, groups are returned as expected. But, when accessed via group.chains no records seem to be returned (just the CollectionProxy object). Trying to manually `to_a` that proxy results in a `elation "groups_queues" does not exist` error again. – BM5k Jul 13 '18 at 23:50
  • [Taking it another step further](https://gitlab.com/snippets/1732932) and defining the join table on *both* databases, the `relation "groups_queues" does not exist` error goes away and it seems to work, until you reload the group. As far as I can tell, when trying to access `group.chains`, rails expects the association data to come from the legacy database BUT when using `group.chains` rails is looking in the modern database. Unfortunately this looks like either a bug in rails OR this type of association simply isn't supported. – BM5k Jul 13 '18 at 23:57
  • [Looking deeper into has_many_through](https://gitlab.com/snippets/1732949) is also not promising. The join model seems to be able access both sides, but the side without the join table still causes problems. – BM5k Jul 14 '18 at 00:05

1 Answers1

0

Because of the problems that rails seems to have with this type of association, I recommend an alternative approach. Copy/import the data from the legacy database into the rails app or wrap the legacy database in its own app and present it to rails via an api instead of connecting to multiple databases from inside rails.

Otherwise it seems like your best bet is to use the has_many :through approach but remember to never call the associations except through the context of the join model.

BM5k
  • 1,210
  • 10
  • 28