gpt4 book ai didi

ruby-on-rails - ActiveRecord::Fixture::FixtureError: 表 "books"没有名为 "loves"的列

转载 作者:行者123 更新时间:2023-12-04 07:29:12 27 4
gpt4 key购买 nike

奇怪的 Rails 问题。

我有一个 Book实体和用户可以Love一本书。

我的所有其他模型都很好,并且通过了所有测试,但是在生成新的 Love 之后模型和设置固定装置,我突然收到大量这些错误:

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

我不知何故认为这可能与 Rails 中的保留关键字有关?

Love任何机会保留关键字?

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

https://reservedwords.herokuapp.com/

和字 love没有结果。

爱情迁移文件
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

图书 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

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

章节 fixture
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

评论 fixture
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

消息 fixture
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

可能是什么问题呢?

更新

如果我注释掉我的爱情装置:
# 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 来自?

我四倍检查了我的爱的拼写。我什至对“Lofe”这个词进行了整个项目搜索,但没有显示任何匹配的搜索结果。

最佳答案

疯狂猜测 ,但我认为,这是因为 Inflector 对待您的has_many :loves线。

我认为它假设 loves 中的单数是 lofe ,因此出现错误,因为它试图解析常量 LofeBook 范围内类(class)。

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

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

关于ruby-on-rails - ActiveRecord::Fixture::FixtureError: 表 "books"没有名为 "loves"的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40846420/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com