2017-06-05 8 views
0

ユーザーモデルと、attendee_idがユーザーを参照する出席モデルとの間の関連付けを作成しようとしています。この関連付けは、ユーザーとコンサートの間の多種多様な関係です。参加テーブルの参加者には、 :attendee:concertという2つのフィールドがあります。ActiveModel :: 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 

は移行されている:私はbundle exec rake db:drop実行

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:createそしてbundle exec rake db:migrate私はデータベースをシードできました。

2

そしてそれ `sは、私が思うに、問題が解決しない場合はテストbundle exec rails db:test:prepare

ため

bundle exec rails db:resetdb:drop + db:create + db:migrate + db:seedと同じ)はattendee_id列がないように見えます

関連する問題