gpt4 book ai didi

ruby - ActiveModel::MissingAttributeError: 无法写入未知属性 `attendee_id`

转载 作者:行者123 更新时间:2023-11-29 14:36:13 25 4
gpt4 key购买 nike

我正在尝试在用户模型和出勤模型之间创建关联,其中 attendee_id 引用用户。该关联是用户和音乐会之间的多对多关系。连接表 attendances 有两个字段:attendee:concert

seed.rb 文件:

require 'faker'

Concert.destroy_all
User.destroy_all
Attendance.destroy_all

15.times do
Concert.create(band: Faker::RockBand.name, venue: "#{Faker::LordOfTheRings.location}", date: Faker::Date.forward(rand(30)), start_time: "8:00 PM")
end

5.times do |number|
User.create(first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, email: "#{number}@email.com", password: "password")
end

concerts = Concert.all
users = User.all

15.times do
Attendance.create(attendee: users.sample, concert: concerts.sample)
end

模型如下:

class Attendance < ApplicationRecord
belongs_to :attendee, class_name: "User"
belongs_to :concert
end
class Concert < ApplicationRecord
validates :band, :venue, :date, :start_time, presence: true
has_many :attendances
has_many :attendees, through: :attendances
end
class User < ApplicationRecord
validates :first_name, :last_name, :email, presence: true
validates_format_of :email, with: /@/
has_secure_password
has_many :attendances, foreign_key: :attendee_id
has_many :concerts, through: :attendances
end

这里是迁移:

class CreateAttendances < ActiveRecord::Migration[5.0]
def change
create_table :attendances do |t|
t.references :attendee
t.references :concert, index: true

t.timestamps
end
end
end

class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :email
t.string :password_digest

t.timestamps
end
end
end

class CreateConcerts < ActiveRecord::Migration[5.0]
def change
create_table :concerts do |t|
t.string :band
t.string :venue
t.datetime :date
t.time :start_time

t.timestamps
end
end
end

最佳答案

看起来没有 attendee_id

bundle exec rails db:reset(与 db:drop + db:create + db:migrate 相同> + db:种子)

对于测试 bundle exec rails db:test:prepare

如果这不能解决问题,我想,我们需要 moar stacktraces)

关于ruby - ActiveModel::MissingAttributeError: 无法写入未知属性 `attendee_id`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44360802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com