Dragos
Dragos

Reputation: 776

Rails table join with extra details doesn't work

I have these 2 models, and I'm trying to add an association between them so I can add some extra details later:

class Ticket < ActiveRecord::Base
  belongs_to :user

  has_many :event_tickets
  has_many :events, :through => :event_tickets
end

class Event < ActiveRecord::Base
  has_many :event_tickets
  has_many :tickets, :through => :event_tickets
end

class EventTicket < ActiveRecord::Base
  belongs_to :event
  belongs_to :ticket
end

I've tried adding an association like this, just to see it's working:

event_ticket = EventTicket.new(:ticket => ticket, :event => Event.all[0])
event_ticket.save!

This gives me:

can't write unknown attribute `ticket_id`

The ticket and event exist and are working fine, but can't get the event_ticket object working.

What am I doing wrong?

Upvotes: 1

Views: 36

Answers (1)

Paul A Jungwirth
Paul A Jungwirth

Reputation: 24581

It sounds like you don't have an event_tickets table, or at least it doesn't have a ticket_id column. Are you sure you wrote the migration and ran it?

Upvotes: 2

Related Questions