Boris
Boris

Reputation: 557

Relation has_many/belongs_to PG::UndefinedColumn

I get this error when I do this:

user.owned_tipsters

Error:

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

My code:

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

If you can help me to know where does this error, I would thank you

Boris Thx

Upvotes: 1

Views: 289

Answers (1)

Bartłomiej Gładys
Bartłomiej Gładys

Reputation: 4615

add foreign_key in you user.rb

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

Upvotes: 2

Related Questions