Jason Gordon
2006-Nov-14 19:08 UTC
Through associations on tables with non-standard primary keys
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:in
`execute''
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:in
`select_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:in
`find_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:in
`find_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:in
`method_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:in `output_value''
from /sw/lib/ruby/1.8/irb.rb:151:in `eval_input''
from /sw/lib/ruby/1.8/irb.rb:259:in `signal_status''
from /sw/lib/ruby/1.8/irb.rb:147:in `eval_input''
from /sw/lib/ruby/1.8/irb.rb:146:in `eval_input''
from /sw/lib/ruby/1.8/irb.rb:70:in `start''
from /sw/lib/ruby/1.8/irb.rb:69:in `start''
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Jason Gordon
2006-Nov-14 20:49 UTC
Re: Through associations on tables with non-standard primary keys
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 On Nov 14, 2:08 pm, "Jason Gordon" <jason.gor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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:in > `execute'' > 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:in > `select_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:in > `find_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:in > `find_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:in > `method_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:in `output_value'' > from /sw/lib/ruby/1.8/irb.rb:151:in `eval_input'' > from /sw/lib/ruby/1.8/irb.rb:259:in `signal_status'' > from /sw/lib/ruby/1.8/irb.rb:147:in `eval_input'' > from /sw/lib/ruby/1.8/irb.rb:146:in `eval_input'' > from /sw/lib/ruby/1.8/irb.rb:70:in `start'' > from /sw/lib/ruby/1.8/irb.rb:69:in `start''--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---