Search code examples
ruby-on-railsrubyrails-activerecordhas-manybelongs-to

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


Solution

  • 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