Foreign key re-assignment does not work in :belongs_to

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

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.

You need to set the :foreign_key option on the has_many too.

Fred

Spot on, thanks. SO simple when you know how! Martin