Forum: Ruby on Rails Record retrieval in Many-to-many using :through not working

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
82ee2cbbd7a6ea331728565f766b0ff3?d=identicon&s=25 Vaishal Sheth (Guest)
on 2006-04-06 19:52
(Received via mailing list)
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
D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2006-04-06 20:17
(Received via mailing list)
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
82ee2cbbd7a6ea331728565f766b0ff3?d=identicon&s=25 Vaishal Sheth (Guest)
on 2006-04-06 20:29
(Received via mailing list)
No, that's not the way to set a many-to-many using through.

I am using this
http://media.rubyonrails.org/presentations/pursuit... (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.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-04-06 21:43
(Received via mailing list)
Vaishal Sheth 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.
82ee2cbbd7a6ea331728565f766b0ff3?d=identicon&s=25 Vaishal Sheth (Guest)
on 2006-04-06 22:02
(Received via mailing list)
Thanks a LOT Mark!!

I could have looked at it all night and never caught that.
This topic is locked and can not be replied to.