ActiveRecord :: Fixture :: FixtureError:表"books"没有名为"loves"的列, [英] ActiveRecord::Fixture::FixtureError: table "books" has no column named "loves"

查看:64
本文介绍了ActiveRecord :: Fixture :: FixtureError:表"books"没有名为"loves"的列,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bizarre Rails问题.

Bizarre Rails problem.

我有一个Book实体,用户可以Love一本书.

I have a Book entity and users can Love a book.

我所有其他模型都很好,并通过了所有测试,但是在生成新的Love模型并设置了固定装置之后,我突然发现了很多错误:

All my other models are fine and passing all the tests but after generating the new Love model and setting the fixtures, I am getting a ton of these errors out of the blue:

ActiveRecord::Fixture::FixtureError: table "books" has no column named "loves".

我可能以某种方式认为与Rails中的保留关键字有关?

I somehow think it's to do with reserved keywords in Rails perhaps?

Love是否是保留关键字?

我在此页面上进行了搜索:

I did a search on this page:

https://reservedwords.herokuapp.com/

love这个词没有任何结果.

And the word love yields no results.

class CreateLoves < ActiveRecord::Migration[5.0]
  def change
    create_table :loves do |t|
      t.references :book, foreign_key: true
      t.references :sender, foreign_key: true
      t.references :receiver, foreign_key: true

      t.timestamps
    end
  end
end

爱情模型

class Love < ApplicationRecord
  belongs_to :book
  belongs_to :sender, class_name: "User"
  belongs_to :receiver, class_name: "User"
end

爱的装置

one:
  book: one
  sender: jim
  receiver: sarah

two:
  book: two
  sender: sarah
  receiver: jim

three:
  book: three
  sender: jim
  receiver: tycus

图书模型

class Book < ApplicationRecord
  belongs_to :author, class_name: "User"
  has_and_belongs_to_many :genres
  has_many :loves
end

书架

one:
  title: Back To The Future
  adult_content: true
  author: jim
  published: false
  genres: action, comedy, science_fiction

two:
  title: Harry Potter and The Philosopher's Stone
  adult_content: false
  author: sarah
  published: false
  genres: action, fantasy

three:
  title: Back To The Future 2
  adult_content: true
  author: jim
  published: false
  genres: action, comedy, science_fiction

four:
  title: Back To The Future 3
  adult_content: true
  author: jim
  published: false
  genres: action, comedy, science_fiction

five:
  title: Royal Guard
  adult_content: true
  author: chewedon
  published: false
  genres: action, romance, fantasy

Books Controller

class BooksController < ApplicationController
  before_action :authenticate_user
  after_action :verify_authorized, except: [:index, :create]
  after_action :verify_policy_scoped, only: :index

  def index
    books = policy_scope(Book.all)
    render json: books
  end

  def create
    book = Book.new(book_params)

    if book.save
      render json: book, status: :created
    else
      render status: :unprocessable_entity
    end
  end

  def show
    book = Book.find_by(id: params[:id])

    if book.present?
      authorize book
      render json: book
    else
      skip_authorization
      render status: :not_found
    end
  end

  def update
    book = Book.find_by(id: params[:id])

    skip_authorization and render status: :not_found and return unless book

    authorize book

    if book.update!(book_params)
      render json: book
    else
      render status: :unprocessable_entity
    end
  end

  def destroy
    book = Book.find_by(id: params[:id])

    skip_authorization and render status: :not_found and return unless book

    authorize book

    if book.destroy!
      render status: :ok
    end
  end


  private

  def book_params
    params.permit(:title, :adult_content, :published, :author_id, genre_ids: [])
  end
end

这是我的其他一些型号及其测试装置,它们都通过了测试:

Here are some of my other model and their test fixtures, they all pass the tests:

one:
  title: Straw House
  order: 1
  content: First pig built a house out of straw
  published: true
  book: one

two:
  title: Wooden House
  order: 2
  content: Second pig built a house out of wood
  published: true
  book: one

three:
  title: Brick House
  order: 3
  content: Third pig built a house out of brick
  published: false
  book: one

four:
  title: Hogwarts School of Wizardry
  order: 1
  content: They pushed their troller into nine and three quarters
  published: false
  book: two

评论治具

one:
  content: Love this chapter
  author: jim
  book: one
  chapter: one

two:
  content: Hate this chapter
  author: sarah
  book: two
  chapter: two

three:
  content: Interesting chapter
  author: tycus
  book: one
  chapter: one

消息装置

one:
  subject: Next Chapter
  body: When will the next chapter be published?
  sender_read: false
  receiver_read: false
  sender: jim
  receiver: sarah

two:
  subject: Film Adaptation
  body: I would like to turn your story into a film
  sender_read: false
  receiver_read: false
  sender: sarah
  receiver: jim

three:
  subject: Mind Blown
  body: Where do you get all these ideas? I'll stalk you now!
  sender_read: false
  receiver_read: false
  sender: tycus
  receiver: jim

可能是什么问题?

如果我注释掉我的恋爱装置,则:

If I comment out my Love Fixtures:

# one:
#   book: one
#   sender: jim
#   receiver: sarah
#
# two:
#   book: two
#   sender: sarah
#   receiver: jim
#
# three:
#   book: three
#   sender: jim
#   receiver: tycus

我收到新的测试错误:

Error:
BooksControllerTest#test_book_show_-_should_show_book_info:
NameError: uninitialized constant Book::Lofe
    app/controllers/books_controller.rb:26:in `show'
    test/controllers/books_controller_test.rb:63:in `block in <class:BooksControllerTest>'

Lofe 到底来自哪里?

Where the heck did Lofe come from?

我将爱"的拼写检查了四倍.我什至在整个项目中搜索了"Lofe"一词,但没有找到匹配的搜索结果.

I quadruple checked my spelling of Love. I even did an entire project search for word "Lofe" and it doesn't show any search results matching.

推荐答案

疯狂的猜测 ,但我认为,这是因为我认为它假定loves中的单数是lofe,因此是错误,因为它试图解析Book类范围内的常量Lofe.

I think it assumes, that singular from loves is lofe, thus the error, because it tries to resolve the constant Lofe in the scope of Book class.

要覆盖默认行为,请参阅文档的第一段代码:

To override the default behaviour, see the very first block of code from the docs:

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.irregular 'octopus', 'octopi'
  # for your case it's
  inflect.irregular 'love', 'loves'
end

这篇关于ActiveRecord :: Fixture :: FixtureError:表"books"没有名为"loves"的列,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆