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:inconst_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:inconst_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:incompute_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:infind_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:inmethod_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:inoutput_value' from c:/ruby/lib/ruby/1.8/irb.rb:151:in
eval_input’
from c:/ruby/lib/ruby/1.8/irb.rb:259:insignal_status' from c:/ruby/lib/ruby/1.8/irb.rb:147:in
eval_input’
from c:/ruby/lib/ruby/1.8/irb.rb:146:ineval_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