Reaching into a has_and_belongs_to_many - will :through work?

Hi. I'm hoping somebody can tell me that I've just gotten my syntax wrong and I can in fact do what I want to do.

I've got a Group, and a Group has_many Campaigns.

Campaign has_and_belongs_to_many Users (and, naturally, vice versa).

So I've got a groups table, a campaigns table (that has a group_id), a campaigns_users join table (with campaign_id and user_id), and a users table (who basically is left not belonging to anyone).

My question is this -- starting from Group I want to reach through that HABTM and point directly at the Users who belong to this Group. I've successfully done it by writing my own finder_sql, or by just cheating and collapsing all the camaign.users together. But I'm working on a user interface for this and I'd like my list of users to behave just like any other activerecord operation so that I can page through them, sort arbitrarily on columns and so on exactly as if I'd originally just had "Group has many Users".

I was hoping that this would work:

class Group   has_many :campaigns   has_many :users, :through=>:campaigns

class Campaign   has_and_belongs_to_many :users

But it doesn't, all I ever get is ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_and_belongs_to_many for has_many :users, :through => :campaigns. Use :source to specify the source reflection.

However I don't understand what it wants in the :source option. It's not like I mucked around with any class_names. (Well, technically this is inheriting an old database so the actual table names are being overridden, but I'm hoping that's not it! it's not as if I'm getting bad SQL generated because an assumed table name does not exist).

Thanks!

What version of rails do you use? This setup works fine for me on 3.1.3 and 3.2.1

Also, try this: has_many :users, :through => :campaigns, :source => :users

rwz wrote in post #1043018:

What version of rails do you use? This setup works fine for me on 3.1.3 and 3.2.1

This is on version 3.0.10. Since it should work and seems to work for you I thought maybe the altered table names could be causing a problem, but I set up a new app with just these three relationships and the traditional table naming conventions and it's still causing a problem. :-/

I'll see if I can whip up a virtual machine to try an updated version of Rails. Thanks for the idea.

Duane Morin wrote in post #1043076:

rwz wrote in post #1043018:

What version of rails do you use? This setup works fine for me on 3.1.3 and 3.2.1

This is on version 3.0.10. Since it should work and seems to work for you I thought maybe the altered table names could be causing a problem, but I set up a new app with just these three relationships and the traditional table naming conventions and it's still causing a problem. :-/

I'll see if I can whip up a virtual machine to try an updated version of Rails. Thanks for the idea.

I'll be darned ... that's it exactly. I took the exact code that failed on Rails 3.0.10 and updated my Gemfile to use 3.1.0, and it worked fine. Great, now I have to go see whether my project which is heavily dependent on legacy stuff can support having their Rails version upgraded. :-/

(On a related note, Vagrant is wonderful for this sort of thing! :))