我有三种模型构成基本的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_id
和ride_id
列,并为其各自的表分配了外键。
我已经坚持了一个星期,对于可能发生这种情况的任何帮助,我们将不胜感激!