# Has\_and\_belongs\_to\_many on different databases

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-on-different-databases/177404>\
**Category:** Rails\
**Created:** [October 16, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-on-different-databases/177404 "2009-10-16T12:18:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erzapito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4491bb/32.png) [@erzapito](https://www.ruby-forum.com/u/erzapito)\
**Post date:** [October 16, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-on-different-databases/177404/1 "2009-10-16T12:18:20Z")

</div>

Hi, I’m working with two models that have a “has\_and\_belongs\_to\_many”  
relation. Both models reside on a database that it is not the default  
one.

A \< External  
has\_and\_belongs\_to\_many: B

B \< External  
has\_and\_belongs\_to\_many: A

Retriving the information for A works perfectly, but when trying to  
“create” an element on A:

“A.B.create(…)”

ActiveRecord tries to create it on the default database. The solution  
was doing:

b = B.find(…)  
A.B \<\< b

But, looking on the sourcecode I’ve found on the ActiveRecord file  
“has\_and\_belongs\_to\_many\_association.rb [25:29]”

```
  def has_primary_key?
    return @has_primary_key unless @has_primary_key.nil?
    @has_primary_key =

```

(ActiveRecord::Base.connection.supports\_primary\_key? &&  
ActiveRecord::Base.connection.primary\_key(@reflection.options  
[:join\_table]))  
end

Shouln’t it be something like?

```
  def has_primary_key?
    return @has_primary_key unless @has_primary_key.nil?
    @has_primary_key = (@owner.connection.supports_primary_key? &&
      @owner.connection.primary_key(@reflection.options

```

[:join\_table]))  
end

Thank you

---

<div class="post-metadata">

**Author:** ![erzapito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4491bb/32.png) [@erzapito](https://www.ruby-forum.com/u/erzapito)\
**Post date:** [October 16, 2009, 12:32pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-on-different-databases/177404/2 "2009-10-16T12:32:59Z")

</div>

On Oct 16, 10:29 am, erzapito [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, I’m working with two models that have a “has\_and\_belongs\_to\_many”  
> relation. Both models reside on a database that it is not the default  
> one.

Have you tried using has many through ? with has many through the join  
table is also a rails model, so you have a place to say that the join  
table should be in this other database.

Fred

---

<div class="post-metadata">

**Author:** ![erzapito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4491bb/32.png) [@erzapito](https://www.ruby-forum.com/u/erzapito)\
**Post date:** [October 16, 2009, 12:41pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-on-different-databases/177404/3 "2009-10-16T12:41:11Z")

</div>

On Fri, Oct 16, 2009 at 12:31 PM, Frederick C. \<  
removed\_email\_address@domain.invalid\> wrote:

> Have you tried using has many through ? with has many through the join  
> table is also a rails model, so you have a place to say that the join  
> table should be in this other database.

I tried it, but doing some tests I found that it allowed to have more  
than  
one relation between two elements, a new problem that I don’t know how  
to  
solve.

The real problem was with the library file, deleting elements also fails  
because it tries to use the base connection instead of the ancestor’s  
one.

–  
removed\_email\_address@domain.invalid @ google talk 😃

---

<div class="post-metadata">

**Author:** ![erzapito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4491bb/32.png) [@erzapito](https://www.ruby-forum.com/u/erzapito)\
**Post date:** [October 16, 2009, 5:46pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-on-different-databases/177404/4 "2009-10-16T17:46:05Z")

</div>

Alexandre Paz Mena wrote:

> On Fri, Oct 16, 2009 at 12:31 PM, Frederick C. \<  
> removed\_email\_address@domain.invalid\> wrote:
> 
> > Have you tried using has many through ? with has many through the join  
> > table is also a rails model, so you have a place to say that the join  
> > table should be in this other database.
> 
> I tried it, but doing some tests I found that it allowed to have more  
> than  
> one relation between two elements, a new problem that I don’t know how  
> to  
> solve.

validates\_uniqueness\_of and a unique index in the DB. Done.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
