Search code examples
ruby-on-railsruby-on-rails-5

Rails: underscore in table name confuses fixtures


I have a rails application with 3 models, OrderItem, Photo and PhotoSize

From schema.rb, this is my table order_item (I removed here irrelevant columns):

  create_table "order_items", force: :cascade do |t|
    t.integer "order_id"
    t.integer "photo_size_id"
    t.integer "photo_id"
    t.index ["order_id"], name: "index_order_items_on_order_id"
    t.index ["photo_id"], name: "index_order_items_on_photo_id"
    t.index ["photo_size_id"], name: "index_order_items_on_photo_size_id"
  end

The generate fixture file looks like:

one:
  order: one
  photo_size: one
  photo: one

two:
  order: two
  photo_size: two
  photo: two

Now running my tests I'm getting the error

ActiveRecord::Fixture::FixtureError: table "order_items" has no column named "photo_size".

Now having photo in the fixture with the corresponding photo_id in the table works fine. photo_size with its corresponding photo_size_id throws an error though. Can any one explain this discrepancy, and how can this problem be fixed?

I tried the suggestion here of adding a statement for how to pluralize photo_size, but this didn't fix the problem. Commenting out the fixture file stops the error naturally as expected though.


Solution

  • Do you have a belongs_to :photo_size in your OrderItem model?