2016-09-22 112 views
0

我得到这个错误当我这样做:关系的has_many/belongs_to的PG :: UndefinedColumn

user.owned_tipsters 

错误:

ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERREUR: column tipsters.user_id does not exist 

我的代码:

class User < ActiveRecord::Base 

    has_many :owned_tipsters, class_name: 'Tipster', inverse_of: :owner 
end 

class Tipster < ActiveRecord::Base 

    belongs_to :owner, class_name: 'User', inverse_of: :owned_tipsters 
end 

如果你能帮助我知道这个错误在哪里,我会感谢你

鲍里斯 THX

+0

你创建在''tipsters'东西users' foregin_key? –

+0

我在提示器中添加了字段'owner_id',但我总是错误 –

回答

2

附加foreign_key在你user.rb

class User < ActiveRecord::Base 
    has_many :owned_tipsters, class_name: 'Tipster', inverse_of: :owner, foreign_key: 'owner_id' 
end 
+0

非常感谢您,但通常我们不必指定'foreign_key'? –

+2

@BorisBresciani当你的名字不符合约定时,你需要':foreign_key'选项:http://guides.rubyonrails.org/association_basics.html#options-for-has-many-foreign-key –

相关问题