Zha*_*ang 3 unit-testing ruby-on-rails fixtures ruby-on-rails-5
奇怪的Rails问题.
我有一个Book实体,用户可以Love一本书.
我所有的其他型号都很好,通过了所有的测试,但在生成新Love模型和设置灯具后,我得到了大量的这些错误:
ActiveRecord::Fixture::FixtureError: table "books" has no column named "loves".
Run Code Online (Sandbox Code Playgroud)
我不知怎的认为这与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
Run Code Online (Sandbox Code Playgroud)
class Love < ApplicationRecord
belongs_to :book
belongs_to :sender, class_name: "User"
belongs_to :receiver, class_name: "User"
end
Run Code Online (Sandbox Code Playgroud)
one:
book: one
sender: jim
receiver: sarah
two:
book: two
sender: sarah
receiver: jim
three:
book: three
sender: jim
receiver: tycus
Run Code Online (Sandbox Code Playgroud)
class Book < ApplicationRecord
belongs_to :author, class_name: "User"
has_and_belongs_to_many :genres
has_many :loves
end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
以下是我的其他模型及其测试装置,它们都通过了测试:
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
可能是什么问题呢?
如果我评论我的爱情装置:
# one:
# book: one
# sender: jim
# receiver: sarah
#
# two:
# book: two
# sender: sarah
# receiver: jim
#
# three:
# book: three
# sender: jim
# receiver: tycus
Run Code Online (Sandbox Code Playgroud)
我收到新的测试错误:
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>'
Run Code Online (Sandbox Code Playgroud)
哎呀Lofe来自哪里?
我四倍检查了我对Love的拼写.我甚至做了整个项目搜索单词"Lofe",它没有显示任何匹配的搜索结果.
疯狂的猜测,但我认为,这是因为如何Inflector对待你的has_many :loves线.
我认为它假定,即从单一loves的lofe,因此错误,因为它试图解决不断Lofe在范围Book类.
要覆盖默认行为,请参阅文档中的第一个代码块:
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'octopus', 'octopi'
# for your case it's
inflect.irregular 'love', 'loves'
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3647 次 |
| 最近记录: |