vendredi 16 décembre 2016

Rails 3 HABTM yields ActiveRecord::Associations::CollectionProxy objects

I have a legacy Rails 3 application and I'm trying to set up a simple HABTM relationship between two models: VolunteerCircle and User. I have this same association in two other models.

The associations are as follows:

VolunteerCircle model:

class VolunteerCircle < ActiveRecord::Base 
    #...

    has_and_belongs_to_many   :users 

    # ...
end

User model:

class User < ActiveRecord::Base
  #...

  has_and_belongs_to_many :volunteer_circles

  #...
end

And here is the migration:

class CreateVolunteerCirclesUsers < ActiveRecord::Migration
  def change
    create_table :volunteer_circles_users, id: false do |t|
      t.belongs_to :volunteer_circle, index: true
      t.belongs_to :user, index: true
    end
  end
end

When I call #users on a VolunteerCircle object (or vice-versa), Rails coughs up an ActiveRecord::Associations::CollectionProxy object.

The volunteer_circles_users table looks right to me in the database. What am I missing here?

Also, I am aware of the superiority of HABTMT. Circumstances beyond my control dictate HABTM here.

Thank you so much!

Aucun commentaire:

Enregistrer un commentaire