0

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:?

Dudo
  • 4,002
  • 8
  • 32
  • 57
  • Yes. Use has_many :through instead of HABTM. It's been replaced. – Marlin Pierce Jan 08 '13 at 20:59
  • Alright... I updated my model... still not linking. Any ideas? – Dudo Jan 09 '13 at 01:03
  • 1
    @MarlinPierce HABTM has *not* been replaced. Its use for join tables with data was deprecated, and there are some trivial caveats regarding its use. See [the Rails docs](http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many). – Dave Newton Jan 09 '13 at 02:08
  • @DaveNewton, I had wondered. I had heard and been encouraged to always give the linking table a significant name and treat it as a first class table, because it might someday be given significant data. However, in experience, sometimes a linking table is just a linking table. :) – Marlin Pierce Jan 09 '13 at 17:49
  • @MarlinPierce Yep; I think the deprecation warnings that were in for HABTM for a few releases were a little misleading in that they didn't highlight that it was only extra-data-linking-tables that HABTM wouldn't work for anymore. It's a rare app that doesn't have *any* just-linking-tables; for those I still really prefer HABTM for its simplicity. – Dave Newton Jan 09 '13 at 17:55
  • @DaveNewton, yet, even apps which have just-linking-tables, you can go pretty far giving them significant names, if you make a dedicated effort to try, and then it seems like there are no just-linking-tables. In any case, it bothers me that HABTM does not support the linking table having an id field or a unique index. We had a many-to-many schema, which turned out to be a one-to-many. I wanted to put a unique index on the linking able to enforce the one-to-many, but HABTM blew up. I assumed this was never changed since Rail was migrating to has-many-through. – Marlin Pierce Jan 09 '13 at 18:00
  • @MarlinPierce HABTM just doesn't support additional data (in a useful way). I suppose you could put DB constraints on columns, but in practice, I've never really had an issue with this. That said, particularly when other apps access the same DB, all sorts of things can go spectacularly wrong. – Dave Newton Jan 09 '13 at 18:02
  • @Mallanaga - did you ever figure it out? I'm having a similar problem (but I don't have an Attendance model, just User and Event) and I have no clue how to attach it to the user that created the event. Any help would be appreciated! – 7ch5 Jan 28 '13 at 17:22
  • has many through:... Event has_many :users, through: :attendances... User has_many :events, through: :attendances. You actually have to make the model / table for attendances. – Dudo Jan 28 '13 at 22:54

0 Answers0