我确信这真的很愚蠢,但我似乎找不到问题所在。
我正在尝试调用Reservation.last.card,但收到错误
Reservation Load (0.3ms) SELECT "reservations".* FROM "reservations" ORDER BY "reservations"."id" DESC LIMIT $1 [["LIMIT", 1]]
NoMethodError: undefined method `card' for #<Reservation:0x090d440e130>
Did you mean? card_id迁移+模式
class AddCardToReservations < ActiveRecord::Migration[5.2]
def change
add_reference :reservations, :card, foreign_key: true
end
end
create_table "reservations", force: :cascade do |t|
t.bigint "park_id"
t.bigint "card_id"
t.index ["card_id"], name: "index_reservations_on_card_id"
t.index ["park_id"], name: "index_reservations_on_park_id"
end模型
class Reservation < ApplicationRecord
has_one :card
belongs_to :park
end
class Card < ApplicationRecord
belongs_to :park
has_many :reservations
end发布于 2019-11-19 20:04:32
Reservation类中的代码行...
has_one :card暗示card对象有一个不是这样的reservation_id,在reservation对象中的外键是card_id,所以您想要的是...
belongs_to :cardhttps://stackoverflow.com/questions/58933148
复制相似问题