You need to tell ActiveRecord the associated column that it is looking for. I'm guessing you want to have the following:
class Person < ActiveRecord::Base
has_many :connection_bindings
has_many :companies, :through => :connection_bindings
has_many :people, :through => :connection_bindings
end
class company < ActiveRecord::Base
has_many :connection_bindings
has_many :companies, :through => :connection_bindings
has_many :people, :through => :connection_bindings
end
The problem there is that you have two tables putting there id in one column and Rails doesn't know which table to look up.
For example, on any given connection_binding row in the database, the connect_from can be either a company_id or a person_id and the same is true of connect_to. So you say: 'Hey Rails, load up my associated ConnectionBindings' and it gets a row where the connect_from is 11 and the connect_to is 12. But does it do Person.find(12) or Company.find(12)? There is no way to tell!
Instead, you'll have to give Rails some more information:
class Person < ActiveRecord::Base
has_many :connection_bindings
has_many :person_connections, :through => :connection_bindings, :source => :to_connect, :source_type => 'Person'
has_many :company_connections, :through => :connection_bindings, :source => :to_connect, :source_type => 'Company
def connections
person_connections + company_connections
end
end
You'll need to build that on the other side (as well as the associated :from_connect's) but it depends on how you're using these connections. Should be enough to get you started.
It is a lot more typing than you're used to in the magical world of Ruby and Rails, but it is a very complex data pattern that you're trying to construct. That doesn't mean it is impossible – Rails, as a good framework, won't stop you from doing anything you really want to – but it is uncommon enough to require some explicitness on your end.