Through associations on tables with non-standard primary key

I cannot seem to get through associations working properly on tables
that have non standard primary keys.

class Address < ActiveRecord::Base
set_primary_key “address_id”
has_many :investor_addresses
has_many :investors, :through => :investor_addresses, :source =>
:investor
end

class Investor < ActiveRecord::Base
set_primary_key “pid”
has_many :investor_addresses
has_many :addresses, :through => :investor_addresses, :source =>
:address
end

class InvestorAddress < ActiveRecord::Base
set_primary_key “investor_address_id”
belongs_to :investor, :foreign_key => “pid”
belongs_to :address, :foreign_key => “address_id”
end

The Investors table has primary key pid.
The Addresses table has primary key address_id.
The investor_addresses table has a primary key investor_address and
also columns pid and address_id.

To me this should work but when I do:
i = Investor.find(6493)
i.addresses

Active record seems to think the investor_addresses table has a column
called investor_id. Why wont it use pid?

ActiveRecord::StatementInvalid: PGError: ERROR: column
investor_addresses.investor_id does not exist
: SELECT addresses.* FROM addresses INNER JOIN investor_addresses ON
addresses.address_id = investor_addresses.address_id WHERE
(investor_addresses.investor_id = 6493)
from
/sw/var/lib/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract_adapter.rb:120:in
log' from /sw/var/lib/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/postgresql_adapter.rb:148:inexecute’
from
/sw/var/lib/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/postgresql_adapter.rb:361:in
select' from /sw/var/lib/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/postgresql_adapter.rb:129:inselect_all’
from
/sw/var/lib/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:390:in
find_by_sql' from /sw/var/lib/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:924:infind_every’
from
/sw/var/lib/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:381:in
find' from /sw/var/lib/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/has_many_through_association.rb:54:infind_target’
from
/sw/var/lib/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/association_proxy.rb:116:in
load_target' from /sw/var/lib/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/association_proxy.rb:109:inmethod_missing’
from
/sw/var/lib/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/has_many_through_association.rb:47:in
method_missing' from /sw/lib/ruby/1.8/irb.rb:298:inoutput_value’
from /sw/lib/ruby/1.8/irb.rb:151:in eval_input' from /sw/lib/ruby/1.8/irb.rb:259:insignal_status’
from /sw/lib/ruby/1.8/irb.rb:147:in eval_input' from /sw/lib/ruby/1.8/irb.rb:146:ineval_input’
from /sw/lib/ruby/1.8/irb.rb:70:in start' from /sw/lib/ruby/1.8/irb.rb:69:instart’

Gonna answer my own question here. It should be as follows:

class Address < ActiveRecord::Base
set_primary_key “address_id”
has_many :investor_addresses, :foreign_key => “address_id”
has_many :investors, :through => :investor_addresses
end

class Investor < ActiveRecord::Base
set_primary_key “pid”
has_many :investor_addresses, :foreign_key => “pid”
has_many :addresses, :through => :investor_addresses
end

class InvestorAddress < ActiveRecord::Base
set_primary_key “investor_address_id”
belongs_to :investor, :foreign_key => “pid”
belongs_to :address, :foreign_key => “address_id”
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs