Martin Hawkins
2009-Jan-05 16:49 UTC
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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Jan-05 16:58 UTC
Re: Foreign key re-assignment does not work in :belongs_to
On 5 Jan 2009, at 16:49, Martin Hawkins wrote:> > 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>--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Martin Hawkins
2009-Jan-05 17:04 UTC
Re: Foreign key re-assignment does not work in :belongs_to
Spot on, thanks. SO simple when you know how! Martin On Jan 5, 4:58 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5 Jan 2009, at 16:49, Martin Hawkins wrote: > > > > > 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 > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---