Rails Seed file not creating Through table


I’m doing the associations project and I can’t seem to get my seeds.rb file to create the records in the through table. I have three models:


class User < ApplicationRecord

    has_many :events, class_name: :Event, foreign_key: :creator_id
    has_many :event_attendances, foreign_key: :attendee_id
    has_many :attended_events, { through: :event_attendances, source: :attended_event }


class Event < ApplicationRecord
    belongs_to :creator, class_name: "User", foreign_key: :creator_id
    has_many :event_attendances, { foreign_key: :event_id }
    has_many :attendees, through: :event_attendances, source: :attendee_id


and EventAttendances.:

class EventAttendance < ApplicationRecord
    belongs_to :attendee, class_name: :User, foreign_key: :attendee_id
    belongs_to :attended_event, { class_name: :Event, foreign_key: :attended_event_id }

EventAttendances should be the through table where users are stored as attendees to various events.

this is my seed.rb file:

def create_event(user)
    user.events.create(    location: Faker::Lorem.word,
                               date: Date.today + 1.day)

3.times do
    user = User.create(name: Faker::Name.first_name)
    event = create_event(user)

  ea = EventAttendance.create(event_id: Event.second.id, attendee_id: User.first.id)
ea_2 = EventAttendance.create(event_id: Event.first.id, attendee_id: User.third.id)
ea_3 = EventAttendance.create(event_id: Event.third.id, attendee_id: User.second.id) 

When I run rails db:reset users and events are created but my eventattendances table is left blank.
ea = EventAttendance.create(event_id: Event.second.id, attendee_id: User.first.id) works in the console. What could be going wrong?


Do you not get any feedback in the console when you ran the seeds file?


here is the output when I reset:

MacBook-Pro-de-Stephen:private_events stephenlang$ rails db:reset
Dropped database 'db/development.sqlite3'
Dropped database 'db/test.sqlite3'
Created database 'db/development.sqlite3'
Created database 'db/test.sqlite3'
-- create_table("event_attendances", {:force=>:cascade})
   -> 0.0108s
-- create_table("events", {:force=>:cascade})
   -> 0.0034s
-- create_table("users", {:force=>:cascade})
   -> 0.0034s
-- create_table("event_attendances", {:force=>:cascade})
   -> 0.0031s
-- create_table("events", {:force=>:cascade})
   -> 0.0020s
-- create_table("users", {:force=>:cascade})
   -> 0.0023s
MacBook-Pro-de-Stephen:private_events stephenlang$ 


Why does it create event_attendances first? Did you create that before any other models?


No I don’t believe so. How can I edit the order of which tables will be created when when I run db:reset?


The migration files dictate the order in which they are run. You haven’t edited them have you?


I may have when I started the project.



It’s a weird one because it obviously is creating the tables you need and if there are no errors during the seeding you can usually expect that it’s completed.

I’m not really sure why it’s happening.