'要求':无法加载此类文件-pry-byebug(LoadError)

时间:2019-11-23 17:46:14

标签: ruby postgresql

我目前正在接受一些培训(所以是新手),并且我试图在Ruby / psql项目中运行console.rb文件。该项目有客户,电影和门票。

该对象是否一旦可以使console.rb文件运行就对数据执行CRUD命令。

目前,三个.rb文件的数据库表为空。

RUBY VERSION:
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]

PSQL VERSION:
psql (PostgreSQL) 11.5

该项目的文件结构如下:

specs
 - film_spec.rb (empty)
 - ticket_spec.rb (empty)
 - customer_spec.rb (empty)

models
 - film.rb
 - ticket.rb
 - customer.rb

db
 - cinema_homework.sql
 - sql_runner.rb
 - customer_spec.rb (empty)

console.rb

控制台文件为:

require('pry')
require_relative('models/customers')
require_relative('models/films')
require_relative('models/tickets')

Tickets.delete_all()
Customers.delete_all()
Films.delete_all()

customer1 = Customers.new({'name' => 'Geddy', 'funds' => 100})
customers1.save()

customer2 etc

customers3 ett


film1 = Films.new({'title' => 'A Show of Hands', 'price' => 20})
film1.save()

film2 etc ...

film3 etc ...

ticket1 = Tickets.new({'customers_id' => customers1.id, 'films_id' => film1.id})
ticket1.save()

ticket2 etc ...

ticket3 etc ...

binding.pry

nil

我收到此错误,但是对于我的一生来说,无法解决正在发生的事情。任何帮助将不胜感激。

Traceback (most recent call last):
    3: from console.rb:2:in `<main>'
    2: from console.rb:2:in `require_relative'
    1: from /Users/johnhannah/codeclan/week_3/day_5/cinema_homework/models/customers.rb:1:in `<top (required)>'
/Users/johnhannah/codeclan/week_3/day_5/cinema_homework/models/customers.rb:1:in `require_relative': cannot load such file -- /Users/johnhannah/codeclan/week_3/day_5/cinema_homework/db/sql_runner (LoadError)

Customers.rb


require_relative('../db/sql_runner.rb')

class Customers

  attr_reader :id
  attr_accessor :name, :funds

  def initialize(options)
    @id = options['id'].to_i if options['id']
    @name = options['name']
    @funds = options['funds'].to_i
  end

  def save()
    sql = "INSERT INTO customers (name, funds)
    VALUES ($1, $2)
    RETURNING id"
    values = [@name, @funds]
    customers = SqlRunner.run(sql, values).first
    @id = customers['id'].to_i
  end

end

1 个答案:

答案 0 :(得分:1)

我建议您将所有文件加载到可执行文件(console.rb)中。

执行此操作时,运行console.rb时所有文件中的所有类都将可用。

因此,在console.rb顶部写类似

require 'pry'
require_relative 'db/sql_runner'
require_relative 'models/film'
require_relative 'models/ticket'
require_relative 'models/customer'

(这些行的顺序可以根据加载类的顺序进行更改)

还要检查课程的单词形式。通常,类名是单数,而不是复数。并检查所有文件名。通常文件名是ClassName的snake_case