{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/18357","labels_url":"https://api.github.com/repos/rails/rails/issues/18357/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/18357/comments","events_url":"https://api.github.com/repos/rails/rails/issues/18357/events","html_url":"https://github.com/rails/rails/issues/18357","id":53479415,"number":18357,"title":"Fixtures behaving flakily","user":{"login":"Joshua-Anderson","id":3526404,"avatar_url":"https://avatars.githubusercontent.com/u/3526404?v=3","gravatar_id":"","url":"https://api.github.com/users/Joshua-Anderson","html_url":"https://github.com/Joshua-Anderson","followers_url":"https://api.github.com/users/Joshua-Anderson/followers","following_url":"https://api.github.com/users/Joshua-Anderson/following{/other_user}","gists_url":"https://api.github.com/users/Joshua-Anderson/gists{/gist_id}","starred_url":"https://api.github.com/users/Joshua-Anderson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Joshua-Anderson/subscriptions","organizations_url":"https://api.github.com/users/Joshua-Anderson/orgs","repos_url":"https://api.github.com/users/Joshua-Anderson/repos","events_url":"https://api.github.com/users/Joshua-Anderson/events{/privacy}","received_events_url":"https://api.github.com/users/Joshua-Anderson/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1"},{"url":"https://api.github.com/repos/rails/rails/labels/needs+feedback","name":"needs feedback","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-01-06T04:16:36Z","updated_at":"2015-01-06T14:32:39Z","closed_at":null,"body":"I'm having a really weird issue with fixtures in my rails project.\r\n\r\nI've tested my project on two fresh, idencial vagrant machines on separate computers and travis-ci.\r\n\r\nOn one of the vagrant machines, this test passes.\r\n\r\n``` ruby\r\n  test 'Why is this test failing?' do\r\n    assert_equal 25, questions(:question_old_6).catagories.length\r\n    assert_equal 6, catagories(:catagory_old_11).questions.length\r\n  end\r\n```\r\n\r\nThe questions and categories models are bound together with a has and belongs to many relation.\r\n\r\n\r\nOn the other vagrant machine and travis-ci, the test fails with:\r\n\r\n``` ruby\r\n 1) Failure:\r\nCatagoriesControllerTest#test_Why_is_this_test_failing? [/home/travis/build/memorizor/memorizor-api/test/controllers/catagories_controller_test.rb:9]:\r\nExpected: 25\r\n  Actual: 0\r\n```\r\n\r\nThe vagrant machines are both freshly booted with only difference between them being that one was started on a different computer.\r\n\r\nI've tried to get the same test to fail on fresh rails projects but haven't been able to reproduce the failures. I've tried to cut down on this code dump as much as possible, but it will still be a little long since this is from my small rails project. It's open source on github at [memorizor/memorizor-api](https://github.com/memorizor/memorizor-api), so you should be able to use this as a large-ish :cry:  reproduction project.\r\n\r\nI'm using ruby 2.2.0 and rails 4.2.0.\r\n\r\n### The Models\r\n\r\n``` ruby\r\nclass Catagory < ActiveRecord::Base\r\n  validates :name, presence: true\r\n  validates :color, presence: true, format: { with: /\\A[a-f0-9]{6}\\z/ }\r\n\r\n  belongs_to :user\r\n  has_and_belongs_to_many :questions\r\n\r\n  def add_questions(question_ids, user)\r\n    question_ids.each do |question_id|\r\n      @question = Question.find_by_id(question_id)\r\n      questions << @question if @question.user == user\r\n    end\r\n  end\r\nend\r\n\r\n\r\nclass Question < ActiveRecord::Base\r\n  validates :content, presence: true\r\n  validates :review_at, presence: true\r\n  validates :answer_type, presence: true, inclusion: { in: [0] }\r\n\r\n  has_many :answers, dependent: :destroy\r\n  belongs_to :user\r\n  has_and_belongs_to_many :catagories\r\nend\r\n```\r\n\r\n### The Database Schema:\r\n\r\n``` ruby\r\nActiveRecord::Schema.define(version: 20141214183400) do\r\n\r\n  # These are extensions that must be enabled in order to support this database\r\n  enable_extension \"plpgsql\"\r\n\r\n  create_table \"answers\", force: true do |t|\r\n    t.text    \"content\",     null: false\r\n    t.integer \"question_id\", null: false\r\n  end\r\n\r\n  add_index \"answers\", [\"question_id\"], name: \"index_answers_on_question_id\", using: :btree\r\n\r\n  create_table \"catagories\", force: true do |t|\r\n    t.string  \"name\",    null: false\r\n    t.string  \"color\",   null: false\r\n    t.integer \"user_id\", null: false\r\n  end\r\n\r\n  add_index \"catagories\", [\"user_id\"], name: \"index_catagories_on_user_id\", using: :btree\r\n\r\n  create_table \"catagories_questions\", id: false, force: true do |t|\r\n    t.integer \"catagory_id\", null: false\r\n    t.integer \"question_id\", null: false\r\n  end\r\n\r\n  create_table \"questions\", force: true do |t|\r\n    t.text     \"content\",     null: false\r\n    t.datetime \"review_at\",   null: false\r\n    t.integer  \"answer_type\", null: false\r\n    t.integer  \"user_id\",     null: false\r\n  end\r\n\r\n  add_index \"questions\", [\"user_id\"], name: \"index_questions_on_user_id\", using: :btree\r\n\r\n  create_table \"users\", force: true do |t|\r\n    t.string  \"name\",                            null: false\r\n    t.string  \"email\",                           null: false\r\n    t.string  \"password_digest\"\r\n    t.boolean \"verified\",        default: false\r\n  end\r\n\r\n  add_index \"users\", [\"email\"], name: \"index_users_on_email\", unique: true, using: :btree\r\n  add_index \"users\", [\"name\"], name: \"index_users_on_name\", unique: true, using: :btree\r\n\r\nend\r\n```\r\n\r\n### Part of the question fixture\r\n\r\n``` yml\r\n<% 50.times do |n| %>\r\nquestion_old_<%= n %>:\r\n  user: super_active_user\r\n  content: '<%= n %>'\r\n  answer_type: 0\r\n  review_at: '2000-01-01 01:00:00 +0000'\r\n<% end %>\r\n\r\n<% 50.times do |n| %>\r\nquestion_new_<%= n %>:\r\n  user: super_active_user\r\n  content: '<%= n %>'\r\n  answer_type: 0\r\n  review_at: '2200-01-01 01:00:00 +0000'\r\n<% end %>\r\n```\r\n\r\n### Part of the catagory fixture\r\n\r\n``` yml\r\n<% 25.times do |n| %>\r\ncatagory_old_<%= n %>:\r\n  user: super_active_user\r\n  questions:\r\n    - question_old_1\r\n    - question_old_2\r\n    - question_old_3\r\n    - question_old_4\r\n    - question_old_5\r\n    - question_old_6\r\n  name: '<%= n %>'\r\n  color: 'ffffff'\r\n<% end %>\r\n\r\n<% 25.times do |n| %>\r\ncatagory_new_<%= n %>:\r\n  user: super_active_user\r\n  questions:\r\n    - question_new_1\r\n    - question_new_2\r\n    - question_new_3\r\n    - question_new_4\r\n    - question_new_5\r\n    - question_new_6\r\n  name: '<%= n %>'\r\n  color: '000000'\r\n<% end %>\r\n```\r\n\r\n### The flakey test\r\n\r\n``` ruby\r\n  test 'Why is this test failing?' do\r\n    assert_equal 25, questions(:question_old_6).catagories.length\r\n    assert_equal 6, catagories(:catagory_old_11).questions.length\r\n  end\r\n```\r\n\r\nThis bug has me had me stumped for weeks. If you need more of the code to solve my issue it's all open source on github at [memorizor/memorizor-api](https://github.com/memorizor/memorizor-api).  If you need any more data or any help with any part of this I am super happy to help. Thank you so much, you guys have done a awesome job with rails! :heart: \r\n"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/68871874","html_url":"https://github.com/rails/rails/issues/18357#issuecomment-68871874","issue_url":"https://api.github.com/repos/rails/rails/issues/18357","id":68871874,"user":{"login":"Joshua-Anderson","id":3526404,"avatar_url":"https://avatars.githubusercontent.com/u/3526404?v=3","gravatar_id":"","url":"https://api.github.com/users/Joshua-Anderson","html_url":"https://github.com/Joshua-Anderson","followers_url":"https://api.github.com/users/Joshua-Anderson/followers","following_url":"https://api.github.com/users/Joshua-Anderson/following{/other_user}","gists_url":"https://api.github.com/users/Joshua-Anderson/gists{/gist_id}","starred_url":"https://api.github.com/users/Joshua-Anderson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Joshua-Anderson/subscriptions","organizations_url":"https://api.github.com/users/Joshua-Anderson/orgs","repos_url":"https://api.github.com/users/Joshua-Anderson/repos","events_url":"https://api.github.com/users/Joshua-Anderson/events{/privacy}","received_events_url":"https://api.github.com/users/Joshua-Anderson/received_events","type":"User","site_admin":false},"created_at":"2015-01-06T14:32:39Z","updated_at":"2015-01-06T14:32:39Z","body":"@rafaelfranca I've been trying to reproduce this bug in other applications, but I haven't managed to yet. The tests always pass locally with fresh projects. Any advice on how to approach making a smaller reproduction case? I will give you the time information when I am at a computer again."}}