关联的has_many的belongs_to(无法识别)

时间:2018-11-03 15:05:54

标签: ruby-on-rails ruby orm

我有三种模型构成基本的has_many through关系:

class Booking < ApplicationRecord
  validates_presence_of :user, :ride, :role, :required_seats
  belongs_to :user
  belongs_to :ride
end

class Ride < ApplicationRecord
    validates_presence_of :origin, :destination, :leave_at, :arrive_at, :price, :seats
    has_many :bookings, dependent: :destroy
  has_many :users, through: :bookings
    accepts_nested_attributes_for :bookings, :allow_destroy => true
end

class User < ApplicationRecord
  has_secure_password
  validates_presence_of :first_name, :last_name, :email, :password_digest
  has_many :bookings, dependent: :destroy
  has_many :rides, through: :bookings
  accepts_nested_attributes_for :bookings, :allow_destroy => true
end

在运行以下模型规格时:

RSpec.describe Booking, type: :model do
  it { should belong_to(:users) }
  it { should belong_to(:rides) }

返回

Failure/Error: it { should belong_to(:users) }
       Expected Booking to have a belongs_to association called users (no association called users)

Failure/Error: it { should belong_to(:rides) }
       Expected Booking to have a belongs_to association called rides (no association called rides)

belongs_to关联显然是在联接模型“ bookings”中建立的,但是在模型中并未被识别。

bookings表具有一个user_idride_id列,并为其各自的表分配了外键。

我已经坚持了一个星期,对于可能发生这种情况的任何帮助,我们将不胜感激!

1 个答案:

答案 0 :(得分:1)

正如arieljuod正确指出的那样,我的规范中的模型应该是单数形式,而不是复数形式。

std::vector<Customer>::iterator

谢谢!