Rails Seed file not creating Through table


#1

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:

Users:

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 }
end

Events:

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

end

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 }
end

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)
end

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


  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?


#2

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


#3

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$ 

#4

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


#5

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


#6

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


#7

I may have when I started the project.


#8

hmmmm,

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.