неизвестный атрибут 'resource_type' для Pin - PullRequest
0 голосов
/ 04 апреля 2020

Я пытаюсь запустить спецификации с помощью rspe c в приложении-клоне Pinterest. Раньше не было этой проблемы, не знаю, что изменилось.

Вот мои миграции.

create_pins

class CreatePins < ActiveRecord::Migration[4.2]
  def change
    create_table :pins do |t|
      t.string :title
      t.string :url
      t.text :text
      t.string :slug
    end
  end
end

add_resource_type_to_pins

class AddResourceTypeToPins < ActiveRecord::Migration[4.2]
  def change
    add_column :pins, :resource_type, :string
  end
end

create_categories

class CreateCategories < ActiveRecord::Migration[4.2]
  def change
    create_table :categories do |t|
      t.string :name
    end

    add_column :pins, :category_id, :integer, references: :categories
    add_index :pins, :category_id

    if Category.all.empty?
      Category.create(name: "ruby")
      Category.create(name: "rails")
      Category.create(name: "unknown")
    end

    unknown_category = Category.find_by_name("unknown")
    pins = Pin.where("category_id is null")

    pins.each do |pin|
      category = Category.find_by_name(pin.resource_type)
      if category.present?
        pin.category_id = category.id
      else
        pin.category_id = unknown_category.id
      end
      pin.save
    end

    if Pin.where("category_id is null").empty?
      remove_column :pins, :resource_type
      puts "MIGRATION SUCCESSFUL!"
      puts "All your data has been migrated successfully."      
    else
      puts "ERROR! Something went wrong - not all pins have been assigned a category Id."
    end
  end
end

add_attachment_image_to_pins

class AddAttachmentImageToPins < ActiveRecord::Migration[5.2]
  def self.up
    change_table :pins do |t|
      t.attachment :image
    end
  end

  def self.down
    remove_attachment :pins, :image
  end
end

схема

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_04_01_220336) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "categories", id: :serial, force: :cascade do |t|
    t.string "name"
  end

  create_table "pins", id: :serial, force: :cascade do |t|
    t.string "title"
    t.string "url"
    t.text "text"
    t.string "slug"
    t.integer "category_id"
    t.string "image_file_name"
    t.string "image_content_type"
    t.bigint "image_file_size"
    t.datetime "image_updated_at"
    t.index ["category_id"], name: "index_pins_on_category_id"
  end

end

seed

Pin.create(
  title: 'Rails Tutorial', 
  url: 'https://www.railstutorial.org/', 
  text: "The trusted standard in self-directed introductions to Ruby on Rails. A complete treatment of all the essentials
    required in most production Rails applications.", 
  slug: "rails-tutorial",
  resource_type: "rails")

Pin.create(
  title: 'Rails for Zombies', 
  url: 'http://railsforzombies.org', 
  text: "A fun, gamified way to hone your Rails skills! Come on...who doesn't like fighting zombies?!", 
  slug: "rails-for-zombies",
  resource_type: "rails")
Pin.create(
  title: 'Try Ruby', 
  url: 'http://tryruby.org/', 
  text: "An interactive, in-browser tutorial for the Ruby programming language. A thorough walkthrough of 
    Ruby's nuances, geared toward beginners.", 
  slug: "try-ruby",
  resource_type: "ruby")
Pin.create(
  title: 'Ruby Quiz', 
  url: 'http://rubyquiz.org', 
  text: "A collection of quizzes on the Ruby programming language.", 
  slug: "ruby-quiz",
  resource_type: "ruby")
Pin.create(
  title: 'Ruby on Rails for Developers', 
  url: 'https://github.com/generalassembly/ga-ruby-on-rails-for-devs', 
  text: "Somewhat advanced curriculum, but the exercises are also good for anyone who is motivated and 
    self-guided with experience.", 
  slug: "ga-ror-for-developers",
  resource_type: "rails")
Pin.create(
  title: 'Ruby Monk', 
  url: 'http://rubymonk.com', 
  text: "The Ruby Primer is a free interactive book that helps you learn Ruby. Discover Ruby idioms, learn 
    lessons and solve problems, all in your browser!",
  slug: "ruby-monk",
  resource_type: "ruby")

Получение ошибки для неизвестного метода "resource_type" при попытке семян загружать.

Приложение работает на Heroku. Что мне не хватает? Дайте мне знать, если нужны другие файлы.

1 Ответ

0 голосов
/ 05 апреля 2020

Удалено resource_type из таблицы Pin при миграции create_categories.rb.

Пришлось изменить seeds.rb resource_type на category_id из новой таблицы Pin.

...