首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pin的未知属性'resource_type‘

Pin的未知属性'resource_type‘
EN

Stack Overflow用户
提问于 2020-04-05 03:11:22
回答 1查看 37关注 0票数 0

我正尝试在Pinterest克隆应用程序上使用rspec运行specs。之前没有这个问题,不确定发生了什么变化。

以下是我的迁移。

create_pins

代码语言:javascript
复制
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

代码语言:javascript
复制
class AddResourceTypeToPins < ActiveRecord::Migration[4.2]
  def change
    add_column :pins, :resource_type, :string
  end
end

create_categories

代码语言:javascript
复制
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

代码语言:javascript
复制
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

模式

代码语言:javascript
复制
# 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

种子

代码语言:javascript
复制
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上运行。我遗漏了什么?如果需要其他文件,请告诉我。

EN

回答 1

Stack Overflow用户

发布于 2020-04-05 05:10:48

create_categories.rb迁移过程中从Pin表中删除了resource_type

我必须将seeds.rb resource_type从新的Pin表更改为category_id

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61033420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档