I've looked and looked... it just doesn't make sense to me yet. I don't know when to use has_and_belongs_to_many
vs has_many through:
So, calendaring. A user creates an event. Some people that are invited can view the event (through a relationship model that isn't relevant here), and if they choose to attend the event, they 'take ownership' of the event. So, a user can have many events, and an event can have many users.
Users...
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
has_many :attendances
has_many :events, through: :attendances
#some other stuff for relationships and validations
Attendances...
class Attendance < ActiveRecord::Base
attr_accessible :event_id, :user_id
belongs_to :event
belongs_to :user
#some other stuff for validations
Events...
class Event < ActiveRecord::Base
attr_accessible :what, :where, :date, :why
has_many :attendances
has_many :users, through: :attendances
#some other stuff for validations
And the events controller...
class EventsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
@event = current_user.events.build(params[:event])
if @event.save
flash[:success] = "Event created!"
redirect_to root_url
else
@feed_items = []
render 'pages/home'
end
end
The events_users table exists. Just the two columns in there.
When I create an event, it get's created successfully, but it's not attached to the user that created it. It's not attached to any user, and the events_users table is blank.
What am I missing? Should I be using has_many through:
?