# Record retrieval in Many-to-many using :through not working

**URL:** <https://www.ruby-forum.com/t/record-retrieval-in-many-to-many-using-through-not-working/57044>\
**Category:** Rails\
**Created:** [April 6, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/record-retrieval-in-many-to-many-using-through-not-working/57044 "2006-04-06T19:52:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vaishal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a3d4f5/32.png) [@Vaishal\_S](https://www.ruby-forum.com/u/Vaishal_S)\
**Post date:** [April 6, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/record-retrieval-in-many-to-many-using-through-not-working/57044/1 "2006-04-06T19:52:45Z")

</div>

Hello,

I get an error while retrieving records from the following model  
structure.

Tables  
foods - id, food  
foodallergies - food\_id, symptom\_id, a few other columns  
symptoms - id, symptom

Models  
class Food \< ActiveRecord::Base  
has\_many :foodallergies  
has\_many :symptoms, :through =\> :foodallergies  
end

class Symptom \< ActiveRecord::Base  
has\_many :foodallergies  
has\_many :foods, :through =\> :foodallergies  
end

class Foodallergy \< ActiveRecord::Base  
belongs\_to :foods  
belongs\_to :symptoms  
end

In the console, when I do  
f = Food.find(:first), the first food item is returned. I can also  
retrieve  
a symptom that way directly from the Symptom model.

But, this does not work.

f = Food.find(:first)  
f.symptoms

f.symptoms generates an error which looks like this

?\> f = Food.find(:first)  
=\> #\<Food:0x39d30b0 @attributes={“food”=\>“pizza”, “id”=\>“29”}\>

> > s = Symptom.find(:first)  
> > =\> #\<Symptom:0x39d2e10 @attributes={“id”=\>“21”, “symptom”=\>“migraine”}\>
> 
> > f.symptoms  
> > NameError: c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0  
> > /lib/active\_support  
> > /dependencies.rb:89:in `const_missing': uninitialized constant Symptoms from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0 /lib/active_recor d/base.rb:1242:in`compute\_type’  
> > from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0  
> > /lib/active\_suppo  
> > rt/dependencies.rb:120:in `const_missing' from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0 /lib/active_suppo rt/dependencies.rb:122:in`const\_missing’  
> > from (eval):1:in `compute_type' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0 /lib/active_recor d/reflection.rb:112:in`klass’  
> > from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0  
> > /lib/active\_recor  
> > d/associations/has\_many\_through\_association.rb:54:in `find_target' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0 /lib/active_recor d/associations/association_proxy.rb:116:in`load\_target’  
> > from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0  
> > /lib/active\_recor  
> > d/associations/association\_proxy.rb:109:in `method_missing' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0 /lib/active_recor d/associations/has_many_through_association.rb:47:in`method\_missing’  
> > from c:/ruby/lib/ruby/1.8/irb.rb:298:in `output_value' from c:/ruby/lib/ruby/1.8/irb.rb:151:in`eval\_input’  
> > from c:/ruby/lib/ruby/1.8/irb.rb:259:in `signal_status' from c:/ruby/lib/ruby/1.8/irb.rb:147:in`eval\_input’  
> > from c:/ruby/lib/ruby/1.8/irb.rb:146:in `eval_input' from c:/ruby/lib/ruby/1.8/irb.rb:70:in`start’  
> > from c:/ruby/lib/ruby/1.8/irb.rb:69:in `start’  
> > from c:/ruby/bin/irb.bat:20\>\>

> From all the examples I have seen so far, f.symptoms should work. And yes,  
> I have the right foreign keys values in foodallergies table.

Any ideas?

Vaishal

---

<div class="post-metadata">

**Author:** ![Vaishal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a3d4f5/32.png) [@Vaishal\_S](https://www.ruby-forum.com/u/Vaishal_S)\
**Post date:** [April 6, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/record-retrieval-in-many-to-many-using-through-not-working/57044/2 "2006-04-06T20:17:23Z")

</div>

One one thing is connected through another, it should belong to it.  
I’m not aware that you can do it the other way around… but it might  
be possible. For example:

Models  
class Food \< ActiveRecord::Base  
belongs\_to :foodallergies  
has\_many :symptoms, :through =\> :foodallergies  
end

---

<div class="post-metadata">

**Author:** ![Vaishal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a3d4f5/32.png) [@Vaishal\_S](https://www.ruby-forum.com/u/Vaishal_S)\
**Post date:** [April 6, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/record-retrieval-in-many-to-many-using-through-not-working/57044/3 "2006-04-06T20:29:49Z")

</div>

No, that’s not the way to set a many-to-many using through.

I am using this  
[http://media.rubyonrails.org/presentations/pursuitofbeauty.pdf](http://media.rubyonrails.org/presentations/pursuitofbeauty.pdf) (Pg 14)  
example by DHH as a guide and it seems I am doing it right.

But I am doing something wrong too…I am not sure what.

---

<div class="post-metadata">

**Author:** ![Vaishal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a3d4f5/32.png) [@Vaishal\_S](https://www.ruby-forum.com/u/Vaishal_S)\
**Post date:** [April 6, 2006, 9:43pm UTC](https://www.ruby-forum.com/t/record-retrieval-in-many-to-many-using-through-not-working/57044/4 "2006-04-06T21:43:50Z")

</div>

Vaishal S. wrote:

> class Foodallergy \< ActiveRecord::Base  
> belongs\_to :foods  
> belongs\_to :symptoms  
> end  
> …  
> NameError: c:/ruby/lib/ruby/gems/1.8/gems/activesupport-  
> 1.3.0/lib/active\_support  
> /dependencies.rb:89:in `const\_missing’: uninitialized constant Symptoms

Use the singular symbols :food and :symptom for your belong\_tos.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Vaishal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a3d4f5/32.png) [@Vaishal\_S](https://www.ruby-forum.com/u/Vaishal_S)\
**Post date:** [April 6, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/record-retrieval-in-many-to-many-using-through-not-working/57044/5 "2006-04-06T22:02:08Z")

</div>

Thanks a LOT Mark!!

I could have looked at it all night and never caught that.
