belongs_to + foreign_key

Having these models:

class League < Tournament   has_many :league_positions, :order => :position, :dependent => :destroy end

class LeaguePosition < ActiveRecord::Base   belongs_to :league, :class_name => 'League', :foreign_key => 'tournament_id'   acts_as_list :scope => :tournament_id end

when I try to destroy a League (so its LeaguePositions too) I get these SQL error:

Unknown column 'league_positions.league_id' in 'where clause': SELECT * FROM league_positions WHERE (league_positions.league_id = 1) ORDER BY position

I thought declaring :foreign_key => 'tournament_id' was enough to let league_positions knows who is its owner. What am I doing wrong?

Having these models:

class League < Tournament   has_many :league_positions, :order => :position, :dependent => :destroy end

class LeaguePosition < ActiveRecord::Base   belongs_to :league, :class_name => 'League', :foreign_key => 'tournament_id'   acts_as_list :scope => :tournament_id end

when I try to destroy a League (so its LeaguePositions too) I get these SQL error:

Unknown column 'league_positions.league_id' in 'where clause': SELECT * FROM league_positions WHERE (league_positions.league_id = 1) ORDER BY position

Does your League table have a field league_positions_id?

Does your LeaguePositions table have a field tournament_id?

-Ben Lisbakken

Excuse me I was a little off. Your League table doesn't need to have a league_positions_id.

The problem is that LeaguePositions knows that it is connected to Leagues by tournament_id. But Leagues think that the key that connects a LeaguePositions to it is called league_positions_id.

this can be fixed in two ways #1 class League < Tournament   has_many :league_positions, :order => :position, :dependent => :destroy, :class => 'LeaguePositions', :foreign_key => 'tournament_id' end

OR

#2 change the foreign key in LeaguePositions from being tournament_id to being league_id. Then change that line in the LeaguePositions class.

-Ben Lisbakken

#1 class League < Tournament   has_many :league_positions, :order => :position, :dependent => :destroy, :class => 'LeaguePositions', :foreign_key => 'tournament_id' end

I've chosen the 2nd option, it works, but I had thought that declaring the :foreign_key and :class_name at belongs_to relationship in LeaguePosition model was enough to tell the framework how the relationship is.

Thanks!