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

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:incompute_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:inconst_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:inklass’
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:inload_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:inmethod_missing’
from c:/ruby/lib/ruby/1.8/irb.rb:298:in output_value' from c:/ruby/lib/ruby/1.8/irb.rb:151:ineval_input’
from c:/ruby/lib/ruby/1.8/irb.rb:259:in signal_status' from c:/ruby/lib/ruby/1.8/irb.rb:147:ineval_input’
from c:/ruby/lib/ruby/1.8/irb.rb:146:in eval_input' from c:/ruby/lib/ruby/1.8/irb.rb:70:instart’
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

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

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 (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.

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.

Thanks a LOT Mark!!

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