Forum: Ruby on Rails Foreign key re-assignment does not work in :belongs_to

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Beeca027396982f7ba3ced79193e5199?d=identicon&s=25 Martin Hawkins (Guest)
on 2009-01-05 17:49
(Received via mailing list)
I have a MS SQL database that I am accessing through ODBC. The tables
do not conform to Rails defaults so I am having to re-assign the
primary keys and foreign keys.
I have the following:
class DimPlayer < ActiveRecord::Base

  set_primary_key "player_id"
  has_many :map_players


end

class MapPlayer < ActiveRecord::Base

  set_primary_key "map_id"

  belongs_to :dim_player, :foreign_key => "to_player_id"


end

When I do a search on MapPlayer, I can use the association with no
problem, e.g.
player_entries = MapPlayer.find(:all, :conditions => ["to_player_id
= ?",  player_id]) returns an array and elements of the array link
back to the DimPlayer table as expected. (player_entries
[0].dim_player)

When I try it the other way,
dim_player_found = DimPlayer.find(:first, :conditions => [ "surname
= ?", "XXXX"])
dim_player_found.map_players gives me an error, such as the one below,
which suggests I'm doing something wrong with the :foreign_key =>
"to_player_id" line.
The error is
S0022 (207) [unixODBC][FreeTDS][SQL Server]Invalid column name
'dim_player_id' which is what I would expect if I had not tried the
foreign_key re-assignment.
I can't see the wood for the trees - any help most appreciated!
regards
Martin
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-01-05 18:00
(Received via mailing list)
On 5 Jan 2009, at 16:49, Martin Hawkins wrote:
> foreign_key re-assignment.
You need to set the :foreign_key option on the has_many too.

Fred
Beeca027396982f7ba3ced79193e5199?d=identicon&s=25 Martin Hawkins (Guest)
on 2009-01-05 18:05
(Received via mailing list)
Spot on, thanks. SO simple when you know how!
Martin

On Jan 5, 4:58 pm, Frederick Cheung <frederick.che...@gmail.com>
This topic is locked and can not be replied to.