Search code examples
ruby-on-railsactiverecordguardbelongs-to

ActiveRecord::Fixture::FixtureError: table "posts" has no column named "user" - where a 'post' belongs_to 'user'


I am trying to create some association tests for a class with a belongs_to association.

The test fails when the fixtures are being created - Rails is not detecting the belongs_to :user association in Posts.

It seems to pass in the regular rake test command, but not when the individual file is ran inside guard.

The test is below:

module Post
  class AssociationsTest < ActiveSupport::TestCase
    subject { Post.new }
    should belong_to(:user)
  end
end

Solution

  • I managed to figure out my issue - I think it was because I was using nested module/class definitions for my test, and using the same module name.

    The working code was:

    class Post::AssociationsTest < ActiveSupport::TestCase
      subject { Post.new }
      should belong_to(:user)
    end