Vaishal Sheth
2006-Apr-06 17:52 UTC
[Rails] 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: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
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060406/73dd517b/attachment-0001.html
njmacinnes@gmail.com
2006-Apr-06 18:14 UTC
[Rails] Record retrieval in Many-to-many using :through not working
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 On 06/04/06, Vaishal Sheth <vpsheth@gmail.com> wrote:> 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 > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Vaishal Sheth
2006-Apr-06 18:27 UTC
[Rails] Record retrieval in Many-to-many using :through not working
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. On 4/6/06, njmacinnes@gmail.com <njmacinnes@gmail.com> wrote:> > 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 > > > On 06/04/06, Vaishal Sheth <vpsheth@gmail.com> wrote: > > 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 > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060406/6d576a94/attachment-0001.html
Mark Reginald James
2006-Apr-06 19:41 UTC
[Rails] Re: Record retrieval in Many-to-many using :through not working
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 SymptomsUse the singular symbols :food and :symptom for your belong_tos. -- We develop, watch us RoR, in numbers too big to ignore.
Vaishal Sheth
2006-Apr-06 19:59 UTC
[Rails] Re: Record retrieval in Many-to-many using :through not working
Thanks a LOT Mark!! I could have looked at it all night and never caught that. On 4/7/06, Mark Reginald James <mrj@bigpond.net.au> wrote:> > 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. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060406/2059ac25/attachment.html