# Once I added this HABTM, one of my 'through' relationships, on a non-habtm model, seems to have brok

**URL:** <https://www.ruby-forum.com/t/once-i-added-this-habtm-one-of-my-through-relationships-on-a-non-habtm-model-seems-to-have-brok/193499>\
**Category:** Rails\
**Created:** [August 18, 2010, 9:05pm UTC](https://www.ruby-forum.com/t/once-i-added-this-habtm-one-of-my-through-relationships-on-a-non-habtm-model-seems-to-have-brok/193499 "2010-08-18T21:05:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 18, 2010, 9:05pm UTC](https://www.ruby-forum.com/t/once-i-added-this-habtm-one-of-my-through-relationships-on-a-non-habtm-model-seems-to-have-brok/193499/1 "2010-08-18T21:05:52Z")

</div>

I’m a rails newb and have been Googling about this, but I’m still  
stumped.

Not showing everything here, but basically it should be a pretty common  
setup so I’m sure others know what I’m doing wrong.

- A meter can belong to many meter\_groups
- A meter\_group can have many meters.
- A user can ‘subscribe’ to viewing a meter\_group (Subscription) so he  
should be able to see the meters that are in that meter\_group of the  
subscription (think subscribing to a topic in a forum.)

Initially I was only allowing a single meter to belong to only one  
meter\_group and the following test and relationship of a Subscription  
worked  
fine:

class Subscription \< ActiveRecord::Base  
belongs\_to :user, :foreign\_key =\> “user\_id”  
belongs\_to :meter\_group, :foreign\_key =\> “meter\_group\_id”  
has\_many :meters, :through =\> :meter\_group  
end

test “group1 and group2 belong to rachel” do  
user = User.find\_by\_login(“rachel”)  
subscriptions = Subscription.find\_all\_by\_user\_id(user.id)  
subscriptions.each do |s|  
meters = s.meters  
meters.each do |m|  
puts “meter = #{p m}”  
end  
end  
end

However now things are not working since I created some HABTM  
relationships  
for meter\_group and meters. I end up with the following error:

ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source  
reflection macro :has\_and\_belongs\_to\_many for has\_many :meters, :through  
=\>  
:meter\_group. Use :source to specify the source reflection.

Here are some of the other relevant tables and migrations (stripped of a  
few  
non-related fields):

class MeterGroup \< ActiveRecord::Base  
belongs\_to :user, :foreign\_key =\> “user\_id”  
has\_and\_belongs\_to\_many :meters  
end

class Meter \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :meter\_groups  
belongs\_to :user, :foreign\_key =\> “user\_id”  
end

#Migrations (removed some cols and drop section)

class CreateMeterGroups \< ActiveRecord::Migration  
def self.up  
create\_table :meter\_groups do |t|  
t.string :name  
t.string :description  
t.integer :user\_id

```
  t.timestamps
end

```

end  
end

class CreateMeters \< ActiveRecord::Migration  
def self.up  
create\_table :meters do |t|  
t.integer :meter\_type\_id  
t.integer :scale\_id  
t.integer :user\_id  
t.string :name  
t.string :description

```
  t.timestamps
end

```

end  
end

class CreateMetersMeterGroups \< ActiveRecord::Migration  
def self.up  
create\_table :meter\_groups\_meters, :id =\> false do |t|  
t.integer :meter\_id  
t.integer :meter\_group\_id

```
  t.timestamps
end

```

end  
end

class CreateSubscriptions \< ActiveRecord::Migration  
def self.up  
create\_table :subscriptions do |t|  
t.integer :user\_id  
t.integer :meter\_group\_id

```
  t.timestamps
end

```

end  
end

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 18, 2010, 9:28pm UTC](https://www.ruby-forum.com/t/once-i-added-this-habtm-one-of-my-through-relationships-on-a-non-habtm-model-seems-to-have-brok/193499/2 "2010-08-18T21:28:07Z")

</div>

On Wed, Aug 18, 2010 at 3:04 PM, Rick R [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> test “group1 and group2 belong to rachel” do

As a side note, there isn’t an issue if I try to get meters directly  
from  
the meter\_group. For example in the above test it works if changed to:

test “group1 and group2 belong to rachel” do  
user = User.find\_by\_login(“rachel”)  
subscriptions = Subscription.find\_all\_by\_user\_id(user.id)  
subscriptions.each do |s|  
_meter\_group = s.meter\_group  
meters = meter\_group.meters_  
meters.each do |m|  
puts “meter = #{p m}”  
end  
end  
end

> class MeterGroup \< ActiveRecord::Base
> 
> end  
> t.timestamps  
> t.timestamps  
> t.timestamps  
> end  
> end  
> end

–  
Rick R

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 18, 2010, 9:32pm UTC](https://www.ruby-forum.com/t/once-i-added-this-habtm-one-of-my-through-relationships-on-a-non-habtm-model-seems-to-have-brok/193499/3 "2010-08-18T21:32:48Z")

</div>

I would start by getting rid of the :foreign\_key specifications  
unless you are doing something non standard.

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 18, 2010, 10:05pm UTC](https://www.ruby-forum.com/t/once-i-added-this-habtm-one-of-my-through-relationships-on-a-non-habtm-model-seems-to-have-brok/193499/4 "2010-08-18T22:05:01Z")

</div>

oh you need to add the has\_many for the join table

has\_many :meter\_goup  
has\_many :meters, :through =\> :meter\_group

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 18, 2010, 9:42pm UTC](https://www.ruby-forum.com/t/once-i-added-this-habtm-one-of-my-through-relationships-on-a-non-habtm-model-seems-to-have-brok/193499/5 "2010-08-18T21:42:46Z")

</div>

On Wed, Aug 18, 2010 at 3:30 PM, Me [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would start by getting rid of the :foreign\_key specifications  
> unless you are doing something non standard.

Ok, thanks. I removed them. (Didn’t fix the issue but does look a lot  
cleaner.)

> > > - A user can ‘subscribe’ to viewing a meter\_group (Subscription) so he  
> > > belongs\_to :meter\_group, :foreign\_key =\> “meter\_group\_id”  
> > > end  
> > > \*meter\_group = s.meter\_group
> 
> a
> 
> > > end  
> > > t.timestamps  
> > > t.string :name  
> > > t.integer :meter\_id  
> > > t.integer :user\_id  
> > > –  
> > > You received this message because you are subscribed to the Google G.  
> > > “Ruby on Rails: Talk” group.  
> > > To post to this group, send email to removed\_email\_address@domain.invalid.  
> > > To unsubscribe from this group, send email to  
> > > removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > > .  
> > > For more options, visit this group at  
> > > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Rick R

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 19, 2010, 12:36am UTC](https://www.ruby-forum.com/t/once-i-added-this-habtm-one-of-my-through-relationships-on-a-non-habtm-model-seems-to-have-brok/193499/6 "2010-08-19T00:36:04Z")

</div>

sorry I misread your tables. The has\_many :through is looking for a  
join table with ids for both sides which is the cause of the error,  
same kind of table as a HABTM join table.

class Author \< ActiveRecord::Base  
has\_many :authorships  
has\_many :books, :through =\> :authorships  
end

class Authorship \< ActiveRecord::Base  
belongs\_to :author  
belongs\_to :book  
end

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 18, 2010, 10:44pm UTC](https://www.ruby-forum.com/t/once-i-added-this-habtm-one-of-my-through-relationships-on-a-non-habtm-model-seems-to-have-brok/193499/7 "2010-08-18T22:44:49Z")

</div>

On Wed, Aug 18, 2010 at 4:00 PM, Me [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> oh you need to add the has\_many for the join table
> 
> has\_many :meter\_goup  
> has\_many :meters, :through =\> :meter\_group

I take it you mean on the Subscription table? which currently looks  
like:

class Subscription \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :meter\_group  
has\_many :meters, :through =\> :meter\_group  
end

But a Subscription doesn’t have many mete groups, a subscription can  
only be  
tied to a single meterGroup and has a fk to the meter\_group table.

Or are you saying for the other join table “meter\_groups\_meters” I need  
to  
make a MeterGroupsMeter model object and add the relationships you  
described?

For reference The other relevant tables are:

class Meter \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :meter\_groups  
belongs\_to :user  
end

class MeterGroup \< ActiveRecord::Base  
belongs\_to :user  
has\_and\_belongs\_to\_many :meters  
end
