2017-06-05 31 views
0

我正嘗試在用戶模型和參與者模型中創建一個參與者id參考用戶的關聯。該協會是用戶和音樂會之間的多對多關係。參加考勤表有兩個字段 :attendee:concertActiveModel :: MissingAttributeError:無法寫入未知屬性`attendee_id`

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 
+0

請刪除代碼段並改用正確的代碼格式(使用th ** {} **按鈕)。另外,請分享產生錯誤的代碼(很可能是您的控制器)。 – Gerry

+0

表示您的某個關聯是錯誤的。 –

+0

@Gerry謝謝你指出。我還沒有創建任何控制器,我只是試圖種子數據庫。 –

回答

0

我跑bundle exec rake db:drop然後我創建的數據庫bundle exec rake db:createbundle exec rake db:migrate我然後能夠種子數據庫。

2

貌似沒有attendee_id

bundle exec rails db:reset(同db:drop + db:create + db:migrate + db:seed

而對於測試bundle exec rails db:test:prepare

如果它`不解決這個問題,我想,我們需要moar堆棧跟蹤)

相關問題